How do functions like alloca allocate memory from the stack?
- Programming
- Security
- Infrastructure
- Developer Tools
The post is a low-level walkthrough of how functions like `alloca` appear to allocate memory “from the stack” even though the stack is not a heap. The key idea is that `alloca` is really compiler or assembly support that adjusts the stack pointer inside a function, then relies on normal function exit to discard that memory. Chen focuses on the Windows wrinkle that large stack growth must be probed page by page so execution cannot skip over the stack’s guard page. That is what helpers like `_chkstk` are for.
If your code uses dynamic stack allocation, audit it now for stack probing and bounded sizes, especially on Windows and with GCC or Clang settings that affect stack-clash protection. More broadly, treat stack allocation as an ABI and compiler feature, not a cute library trick, because portability and security depend on code generation details.
-
devblogs.microsoft.com
- Discuss on HN