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