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 at‐
25 tributes(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 The space allocated by alloca() is not automatically deallocated if the
51 pointer that refers to it simply goes out of scope.
52
53 Do not attempt to free(3) space allocated by alloca()!
54
55 Notes on the GNU version
56 Normally, gcc(1) translates calls to alloca() with inlined code. This
57 is not done when either the -ansi, -std=c89, -std=c99, or the -std=c11
58 option is given and the header <alloca.h> is not included. Otherwise,
59 (without an -ansi or -std=c* option) the glibc version of <stdlib.h>
60 includes <alloca.h> and that contains the lines:
61
62 #ifdef __GNUC__
63 #define alloca(size) __builtin_alloca (size)
64 #endif
65
66 with messy consequences if one has a private version of this function.
67
68 The fact that the code is inlined means that it is impossible to take
69 the address of this function, or to change its behavior by linking with
70 a different library.
71
72 The inlined code often consists of a single instruction adjusting the
73 stack pointer, and does not check for stack overflow. Thus, there is
74 no NULL error return.
75
77 There is no error indication if the stack frame cannot be extended.
78 (However, after a failed allocation, the program is likely to receive a
79 SIGSEGV signal if it attempts to access the unallocated space.)
80
81 On many systems alloca() cannot be used inside the list of arguments of
82 a function call, because the stack space reserved by alloca() would ap‐
83 pear on the stack in the middle of the space for the function argu‐
84 ments.
85
87 brk(2), longjmp(3), malloc(3)
88
90 This page is part of release 5.10 of the Linux man-pages project. A
91 description of the project, information about reporting bugs, and the
92 latest version of this page, can be found at
93 https://www.kernel.org/doc/man-pages/.
94
95
96
97GNU 2019-03-06 ALLOCA(3)