News for hackers. Stuff that matters.
Re:How safe is Zig? by skullt on Thursday June 23, 2022 @04:26PM (#31854709)
Does that not contradict the Zig principle of no hidden allocations?
Re:How safe is Zig? by kristoff_it on Thursday June 23, 2022 @04:45PM (#31854918)
I don't know the precise details of what Andrew has in mind but the compiler can know how much memory is required for this kind of operation at compile time. This is different from normal heap allocation where you only know how much memory is needed at the last minute.

At least in simple cases, this means that the memory for escaped variables could be allocated all at once at the beginning of the program not too differently to how the program allocates memory for the stack.

Re:How safe is Zig? by messe on Thursday June 23, 2022 @06:32PM (#31855907)
Static allocation at the beginning of the program like that can only work for single threaded programs with non-recursive functions though, right?

I’d hazard a guess that the implementation will rely on use-after-free faulting, meaning that the use of any escaped variable will fault rather than corrupting the stack.

Re:How safe is Zig? by wavesquid on Thursday June 23, 2022 @10:57PM (#31857323)
No need to limit to single-threaded: as long as we reserve enough space in the TLS area it's possible to work with multiple threads.

Zig has future plans to require recursive functions to declare their maximum stack memory usage up-front, so that will provide the rest.

Re:How safe is Zig? by adwn on Thursday June 23, 2022 @11:56PM (#31857657)
No need for recursion or multi-threading: If you call the function in a loop and don't release the escaped local unter after the loop, and if the number of loop iterations isn't statically known, then it's impossible to pre-allocate heap storage for that local variable.