1alloca(3)                  Library Functions Manual                  alloca(3)
2
3
4

NAME

6       alloca - allocate memory that is automatically freed
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <alloca.h>
13
14       void *alloca(size_t size);
15

DESCRIPTION

17       The  alloca() function allocates size bytes of space in the stack frame
18       of the caller.  This temporary space is automatically  freed  when  the
19       function that called alloca() returns to its caller.
20

RETURN VALUE

22       The  alloca()  function returns a pointer to the beginning of the allo‐
23       cated space.  If the allocation causes stack overflow, program behavior
24       is undefined.
25

ATTRIBUTES

27       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
28       tributes(7).
29
30       ┌────────────────────────────────────────────┬───────────────┬─────────┐
31Interface                                   Attribute     Value   
32       ├────────────────────────────────────────────┼───────────────┼─────────┤
33alloca()                                    │ Thread safety │ MT-Safe │
34       └────────────────────────────────────────────┴───────────────┴─────────┘
35

STANDARDS

37       None.
38

HISTORY

40       PWB, 32V.
41

NOTES

43       The alloca() function is machine- and compiler-dependent.   Because  it
44       allocates  from  the stack, it's faster than malloc(3) and free(3).  In
45       certain cases, it can also simplify memory deallocation in applications
46       that  use  longjmp(3) or siglongjmp(3).  Otherwise, its use is discour‐
47       aged.
48
49       Because the space allocated by alloca() is allocated within  the  stack
50       frame,  that  space  is  automatically  freed if the function return is
51       jumped over by a call to longjmp(3) or siglongjmp(3).
52
53       The space allocated by alloca() is not automatically deallocated if the
54       pointer that refers to it simply goes out of scope.
55
56       Do not attempt to free(3) space allocated by alloca()!
57
58       By   necessity,   alloca()  is  a  compiler  built-in,  also  known  as
59       __builtin_alloca().  By default, modern compilers automatically  trans‐
60       late  all  uses of alloca() into the built-in, but this is forbidden if
61       standards conformance is requested (-ansi, -std=c*), in which case <al‐
62       loca.h> is required, lest a symbol dependency be emitted.
63
64       The fact that alloca() is a built-in means it is impossible to take its
65       address or to change its behavior by linking with a different library.
66
67       Variable length arrays (VLAs) are part of the  C99  standard,  optional
68       since C11, and can be used for a similar purpose.  However, they do not
69       port to standard C++, and, being variables, live in their  block  scope
70       and  don't  have an allocator-like interface, making them unfit for im‐
71       plementing functionality like strdupa(3).
72

BUGS

74       Due to the nature of the stack, it is impossible to check if the  allo‐
75       cation would overflow the space available, and, hence, neither is indi‐
76       cating an error.  (However, the program is likely to receive a  SIGSEGV
77       signal if it attempts to access unavailable space.)
78
79       On many systems alloca() cannot be used inside the list of arguments of
80       a function call, because the stack space reserved by alloca() would ap‐
81       pear  on  the  stack  in the middle of the space for the function argu‐
82       ments.
83

SEE ALSO

85       brk(2), longjmp(3), malloc(3)
86
87
88
89Linux man-pages 6.04              2023-03-30                         alloca(3)
Impressum