Many
Unix-like systems as well as
Microsoft Windows implement a function called for dynamically allocating stack memory in a way similar to the heap-based
malloc. A compiler typically translates it to inlined instructions manipulating the stack pointer, similar to how
variable-length arrays are handled. Although there is no need to explicitly free the memory, there is a risk of undefined behavior due to stack overflow. The function was present on Unix systems as early as
32/V (1978), but is not part of
Standard C or any
POSIX standard. A safer version of called , which allocates on the heap if the allocation size is too large, and reports stack overflow errors, exists on Microsoft Windows. It requires the use of .
gnulib provides an equivalent interface, albeit instead of throwing an SEH exception on overflow, it delegates to when an overlarge size is detected. A similar feature can be emulated using manual accounting and size-checking, such as in the uses of in glibc. Some processor families, such as the
x86, have special instructions for manipulating the stack of the currently executing thread. Other processor families, including
RISC-V,
PowerPC and
MIPS, do not have explicit stack support, but instead rely on convention and delegate stack management to the operating system's
application binary interface (ABI).
Auto VLAs In addition, since the C version C99 (optional since C11), it is possible to create an array on the stack within a function, automatically, known as an
auto VLA (
variable-length array). It is not supported in
C++, however. void f(int len) { // auto VLA - this array's length is set at // the time of the function invocation / stack generation. int b[len]; // auto VLA - this array's length is set at for (int i = 0; i == See also ==