How alloca() Allocates Stack Memory Without Skipping the Guard Page
How do functions like alloca allocate memory from the stack?

In this post, Raymond Chen explains how the `_alloca()` function allocates memory from the stack on x86-64, specifically addressing whether it performs the necessary stack probing via `_chkstk()`. He confirms that `_alloca()` calls `_chkstk()` to probe the stack before adjusting the stack pointer, just like the compiler does for large local frames. A code example illustrates the assembly instructions, showing the same `__chkstk` function used for both the initial frame setup and the `alloca()` allocation.
Yes, the `_alloca()` function calls the same `_chkstk()` function to probe the stack before adjusting the stack pointer for the allocated memory.
- pjmlp
One of those functions that isn't really implementable in standard C, requiring either compiler support, or being written in straight Assembly for stack registers manipulation, one of those "micro runtime" features for C.
From UNIX 7th edition all the way up to C99, when VLAs where introduced, only to be made optional in C11, and the C23 update still doesn't support automatic VLAs, only for function parameters, thus the point stands.
- eska
Recommended related reading:
- kjellsbells
One thing that scares me a little is whether there are younger developers, say, 25-40, who can and want to pick up the mantle of Windows internals gurus.
I mean, Chen has decades of winternals in his head. Microsoft has been gutting their staff for years now. When the Petzold/Chen generation hang up their spurs, does Microsoft still have a critical mass of people who understand Windows from the metal up?
- rramadass
Related to the above, two important concepts to know w.r.t a stack are "Red Zone" and "Guard Pages".
Raymond Chen again;
Why do we even need to define a red zone? Can’t I just use my stack for anything? - https://devblogs.microsoft.com/oldnewthing/20190111-00/?p=10...
A closer look at the stack guard page - https://devblogs.microsoft.com/oldnewthing/20220203-00/?p=10...
- jacknews
Only passingly related, some fun rust stack-allocation insanity by my 17yo son: