#include <alloca.h> void *alloca(size_t size);
Interface | Attribute | Value |
alloca() | Thread safety | MT-Safe |
alloca() originates from PWB and 32V, and appears in all their derivatives.
Because the space allocated by alloca() is allocated within the stack frame, that space is automatically freed if the function return is jumped over by a call to longjmp(3) or siglongjmp(3).
The space allocated by alloca() is not automatically deallocated if the pointer that refers to it simply goes out of scope.
Do not attempt to free(3) space allocated by alloca()!
By necessity, alloca() is a compiler built-in, also known as __builtin_alloca(). By default, modern compilers automatically translate all uses of alloca() into the built-in, but this is forbidden if standards conformance is requested (-ansi, -std=c*), in which case <alloca.h> is required, lest a symbol dependency be emitted.
The fact that alloca() is a built-in means it is impossible to take its address or to change its behavior by linking with a different library.
Variable length arrays (VLAs) are part of the C99 standard, optional since C11, and can be used for a similar purpose. However, they do not port to standard C++, and, being variables, live in their block scope and don't have an allocator-like interface, making them unfit for implementing functionality like strdupa(3).
On many systems alloca() cannot be used inside the list of arguments of a function call, because the stack space reserved by alloca() would appear on the stack in the middle of the space for the function arguments.