Article

How do functions like alloca allocate memory from the stack?

The Old New Thing ·The Old New Thing ·Published 2026-08-17 ·5 min read

This article explains how the _alloca function works in conjunction with the _chkstk function to manage stack memory allocation. It provides an example of stack allocation in x86-64 architecture.

From the article

A little while ago, I talked about how compilers ensure that large stack allocations do not skip over the guard page . Shawn Van Ness was curious how this works with _alloca . “Does it do the necessary _chkstk() probing?”

Yes, the _alloca() function calls the same _chkstk() function to probe the stack before adjusting the stack pointer for the allocated memory.

#include <malloc.h> void consume(void*,void*); void f(int n) { char buffer[16384]; consume(alloca(n), buffer); } On x86-64, this results in


Share this resource


Discovered 2026-08-18 Source The Old New Thing Archive 2026-08 →