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 For an explanation of the terms used in this section, see
25 attributes(7).
26
27 ┌──────────┬───────────────┬─────────┐
28 │Interface │ Attribute │ Value │
29 ├──────────┼───────────────┼─────────┤
30 │alloca() │ Thread safety │ MT-Safe │
31 └──────────┴───────────────┴─────────┘
33 This function is not in POSIX.1.
34
35 There is evidence that the alloca() function appeared in 32V, PWB,
36 PWB.2, 3BSD, and 4BSD. There is a man page for it in 4.3BSD. Linux
37 uses the GNU version.
38
40 The alloca() function is machine- and compiler-dependent. For certain
41 applications, its use can improve efficiency compared to the use of
42 malloc(3) plus free(3). In certain cases, it can also simplify memory
43 deallocation in applications that use longjmp(3) or siglongjmp(3).
44 Otherwise, its use is discouraged.
45
46 Because the space allocated by alloca() is allocated within the stack
47 frame, that space is automatically freed if the function return is
48 jumped over by a call to longjmp(3) or siglongjmp(3).
49
50 Do not attempt to free(3) space allocated by alloca()!
51
52 Notes on the GNU version
53 Normally, gcc(1) translates calls to alloca() with inlined code. This
54 is not done when either the -ansi, -std=c89, -std=c99, or the -std=c11
55 option is given and the header <alloca.h> is not included. Otherwise,
56 (without an -ansi or -std=c* option) the glibc version of <stdlib.h>
57 includes <alloca.h> and that contains the lines:
58
59 #ifdef __GNUC__
60 #define alloca(size) __builtin_alloca (size)
61 #endif
62
63 with messy consequences if one has a private version of this function.
64
65 The fact that the code is inlined means that it is impossible to take
66 the address of this function, or to change its behavior by linking with
67 a different library.
68
69 The inlined code often consists of a single instruction adjusting the
70 stack pointer, and does not check for stack overflow. Thus, there is
71 no NULL error return.
72
74 There is no error indication if the stack frame cannot be extended.
75 (However, after a failed allocation, the program is likely to receive a
76 SIGSEGV signal if it attempts to access the unallocated space.)
77
78 On many systems alloca() cannot be used inside the list of arguments of
79 a function call, because the stack space reserved by alloca() would
80 appear on the stack in the middle of the space for the function argu‐
81 ments.
82
84 brk(2), longjmp(3), malloc(3)
85
87 This page is part of release 4.15 of the Linux man-pages project. A
88 description of the project, information about reporting bugs, and the
89 latest version of this page, can be found at
90 https://www.kernel.org/doc/man-pages/.
91
92
93
94GNU 2017-09-15 ALLOCA(3)