Section: Linux Programmer's Manual (3)
Return to Main Contents
alloca - allocate memory that is automatically freed
void *alloca(size_t size);
bytes of space in the stack frame of the caller.
This temporary space is
automatically freed when the function that called
returns to its caller.
function returns a pointer to the beginning of the allocated space.
If the allocation causes stack overflow, program behavior is undefined.
This function is not in POSIX.1-2001.
There is evidence that the
function appeared in 32V, PWB, PWB.2, 3BSD, and 4BSD.
There is a man page for it in 4.3BSD.
Linux uses the GNU version.
function is machine- and compiler-dependent.
For certain applications,
its use can improve efficiency compared to the use of
In certain cases,
it can also simplify memory deallocation in applications that use
Otherwise, its use is discouraged.
Because the space allocated by
is allocated within the stack frame,
that space is automatically freed if the function return
is jumped over by a call to
Do not attempt to
space allocated by
Notes on the GNU Version
translates calls to
with inlined code.
This is not done when either the
option is given
(and the header
is not included).
By default the glibc version of
and that contains the line:
#define alloca(size) __builtin_alloca (size)
with messy consequences if one has a private version of this function.
The fact that the code is inlined means that it is impossible
to take the address of this function, or to change its behavior
by linking with a different library.
The inlined code often consists of a single instruction adjusting
the stack pointer, and does not check for stack overflow.
Thus, there is no NULL error return.
There is no error indication if the stack frame cannot be extended.
(However, after a failed allocation, the program is likely to receive a
signal if it attempts to access the unallocated space.)
On many systems
cannot be used inside the list of arguments of a function call, because
the stack space reserved by
would appear on the stack in the middle of the space for the
This page is part of release 3.27 of the Linux
A description of the project,
and information about reporting bugs,
can be found at
- RETURN VALUE
- CONFORMING TO
- Notes on the GNU Version
- SEE ALSO
This document was created by
using the manual pages.