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 -std=c11
46 option is given and the header <alloca.h> is not included. Otherwise
47 (without an -ansi or -std=c* option) the glibc version of <stdlib.h>
48 includes <alloca.h> and that contains the lines:
49
50 #ifdef __GNUC__
51 #define alloca(size) __builtin_alloca (size)
52 #endif
53
54 with messy consequences if one has a private version of this function.
55
56 The fact that the code is inlined means that it is impossible to take
57 the address of this function, or to change its behavior by linking with
58 a different library.
59
60 The inlined code often consists of a single instruction adjusting the
61 stack pointer, and does not check for stack overflow. Thus, there is
62 no NULL error return.
63
65 There is no error indication if the stack frame cannot be extended.
66 (However, after a failed allocation, the program is likely to receive a
67 SIGSEGV signal if it attempts to access the unallocated space.)
68
69 On many systems alloca() cannot be used inside the list of arguments of
70 a function call, because the stack space reserved by alloca() would
71 appear on the stack in the middle of the space for the function argu‐
72 ments.
73
75 brk(2), longjmp(3), malloc(3)
76
78 This page is part of release 3.53 of the Linux man-pages project. A
79 description of the project, and information about reporting bugs, can
80 be found at http://www.kernel.org/doc/man-pages/.
81
82
83
84GNU 2013-05-12 ALLOCA(3)