Ask HN: Why is the stack so constrained?
Large stack allocations are discouraged on modern CPUs because call stacks are placed in relatively small stack segments. If these overflow you get stack overflows. You can use the limit command to see default size of the stack segment on your machine. On mine it is eight megabytes which is almost nothing. :)<p>In &quot;normal&quot; C code, the layout of all call frames (stack frames) is fixed. The location of all local variables relative to the top of the stack is known at compile-time. However, in functions that use alloca, or Variable Length Arrays (VLAs), the layout of the call frame is potentially different in each function call. Therefore, the compiler has to insert code to keep track of where all local variables are and it also makes some compiler optimizations harder.<p>Look at the machine code generated for the following function using godbolt.org. It is way more complicated than if a and b&#x27;s sizes had been compile-time constants:<p><pre><code>    void fun(int x, int y) {
        int a[x], b[y];
        b[5] = 10;
    }
</code></pre>
Dynamically sized call frames are a PITA to implement correctly and efficiently which I suspect is the reason why alloca&#x2F;VLAs are poorly supported in many compilers.<p>There&#x27;s some good discussion here about VLAs and alloca: <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;1018853&#x2F;why-is-the-use-of-alloca-not-considered-good-practice" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;1018853&#x2F;why-is-the-use-o...</a> Although be aware that some of the answers are wrong so read the comments too.<p>Even with good support for VLAs&#x2F;alloca, large stack allocations violates the assumption that the stack segment is large enough. You may be a tidy coder, so you check whether your mallocs returns NULL, but I bet you have never ever checked whether you have enough stack space for a function call. :) Most of the time this works fine because relatively little data is placed on the stack (lots of recursive tree-traversal code, however, breaks badly). So you have to keep track of how much stack space you have left and grow the stack segment if you are nearing exhaustion. For example, if you only have 100 bytes left but want to create a 200 byte local variable, you would allocate a new stack segment, perhaps twice as large as the old one, copy the old stack to the new segment and continue from there. Unfortunately, it would be a major performance drag since you would need to check that you have enough stack space for every dynamic stack allocation and for every call frame.<p>Some virtual machines use guard pages to check for stack overflows. But the amount of overflow they can detect is limited to the size of the guard page. For example, if you have 64 kb guard pages they would be too small to detect overflows caused by 100 kb stack allocations. So you don&#x27;t get around the need for explicit checks.