pedropark99 / zig-book

An open, technical and introductory book for the Zig programming language
https://pedropark99.github.io/zig-book/
Other
590 stars 21 forks source link

Talk about stack overflow on big stack allocations #46

Closed pedropark99 closed 3 weeks ago

pedropark99 commented 1 month ago

This is not a Zig-specific subject. It occurs in any programming language that have manual memory management. Intermediary-advanced C/C++ programmers are usually aware of this problem.

The stack is limited in size. This size depends on a lot of things (architecture, operating system, etc.). In essence, if you try to make an allocation on the stack, that is so big that exceeds the stack size limit, a stack overflow happens, and your program usually crashes.

I'm not sure yet if is a good idea to include a section in the book, to describe this stack overflow problem. But, currently, the Zig compiler does not have a clear error message that identifies this problem. Therefore, when you cause a stack overflow like this in a Zig program, your program normally just crashes with a segmentation fault message, that give you no clue about where the problem is. The Zig team is aware of this problem, but it appears that an improvement on the error messages, or, a any other type of solution has not yet been planned: https://github.com/ziglang/zig/issues/7371

More details on section "Very large stack variables" at: https://en.wikipedia.org/wiki/Stack_overflow