1ALLOCA(3) Linux Programmer's Manual ALLOCA(3)
2
3
4
6 alloca - allocate memory that is automatically freed
7
9 #include <alloca.h>
10
11 void *alloca(size_t size);
12
14 The alloca() function allocates size bytes of space in the stack frame
15 of the caller. This temporary space is automatically freed when the
16 function that called alloca() returns to its caller.
17
19 The alloca() function returns a pointer to the beginning of the allo‐
20 cated space. If the allocation causes stack overflow, program behavior
21 is undefined.
22
24 This function is not in POSIX.1-2001.
25
26 There is evidence that the alloca() function appeared in 32V, PWB,
27 PWB.2, 3BSD, and 4BSD. There is a man page for it in 4.3BSD. Linux
28 uses the GNU version.
29
31 The alloca() function is machine- and compiler-dependent. For certain
32 applications, its use can improve efficiency compared to the use of
33 malloc(3) plus free(3). In certain cases, it can also simplify memory
34 deallocation in applications that use longjmp(3) or siglongjmp(3).
35 Otherwise, its use is discouraged.
36
37 Because the space allocated by alloca() is allocated within the stack
38 frame, that space is automatically freed if the function return is
39 jumped over by a call to longjmp(3) or siglongjmp(3).
40
41 Do not attempt to free(3) space allocated by alloca()!
42
43 Notes on the GNU Version
44 Normally, gcc(1) translates calls to alloca() with inlined code. This
45 is not done when either the -ansi, -std=c89, -std=c99, or the
46 -fno-builtin option is given (and the header <alloca.h> is not
47 included). But beware! By default the glibc version of <stdlib.h>
48 includes <alloca.h> and that contains the line:
49
50 #define alloca(size) __builtin_alloca (size)
51
52 with messy consequences if one has a private version of this function.
53
54 The fact that the code is inlined means that it is impossible to take
55 the address of this function, or to change its behavior by linking with
56 a different library.
57
58 The inlined code often consists of a single instruction adjusting the
59 stack pointer, and does not check for stack overflow. Thus, there is
60 no NULL error return.
61
63 There is no error indication if the stack frame cannot be extended.
64 (However, after a failed allocation, the program is likely to receive a
65 SIGSEGV signal if it attempts to access the unallocated space.)
66
67 On many systems alloca() cannot be used inside the list of arguments of
68 a function call, because the stack space reserved by alloca() would
69 appear on the stack in the middle of the space for the function argu‐
70 ments.
71
73 brk(2), longjmp(3), malloc(3)
74
76 This page is part of release 3.22 of the Linux man-pages project. A
77 description of the project, and information about reporting bugs, can
78 be found at http://www.kernel.org/doc/man-pages/.
79
80
81
82GNU 2008-01-24 ALLOCA(3)