poll.2: Remove <signal.h>
[man-pages.git] / man3 / alloca.3
blob5bceeabe10e8be7317fb15171347f3776ab93749
1 .\" Copyright (c) 1980, 1991 Regents of the University of California.
2 .\" All rights reserved.
3 .\"
4 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\"    notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\"    notice, this list of conditions and the following disclaimer in the
12 .\"    documentation and/or other materials provided with the distribution.
13 .\" 3. All advertising materials mentioning features or use of this software
14 .\"    must display the following acknowledgement:
15 .\"     This product includes software developed by the University of
16 .\"     California, Berkeley and its contributors.
17 .\" 4. Neither the name of the University nor the names of its contributors
18 .\"    may be used to endorse or promote products derived from this software
19 .\"    without specific prior written permission.
20 .\"
21 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 .\" SUCH DAMAGE.
32 .\" %%%LICENSE_END
33 .\"
34 .\"     @(#)alloca.3    5.1 (Berkeley) 5/2/91
35 .\"
36 .\" Converted Mon Nov 29 11:05:55 1993 by Rik Faith <faith@cs.unc.edu>
37 .\" Modified Tue Oct 22 23:41:56 1996 by Eric S. Raymond <esr@thyrsus.com>
38 .\" Modified 2002-07-17, aeb
39 .\" 2008-01-24, mtk:
40 .\"     Various rewrites and additions (notes on longjmp() and SIGSEGV).
41 .\"     Weaken warning against use of alloca() (as per Debian bug 461100).
42 .\"
43 .TH ALLOCA 3 2021-03-22 "GNU" "Linux Programmer's Manual"
44 .SH NAME
45 alloca \- allocate memory that is automatically freed
46 .SH SYNOPSIS
47 .nf
48 .B #include <alloca.h>
49 .PP
50 .BI "void *alloca(size_t " size );
51 .fi
52 .SH DESCRIPTION
53 The
54 .BR alloca ()
55 function allocates
56 .I size
57 bytes of space in the stack frame of the caller.
58 This temporary space is
59 automatically freed when the function that called
60 .BR alloca ()
61 returns to its caller.
62 .SH RETURN VALUE
63 The
64 .BR alloca ()
65 function returns a pointer to the beginning of the allocated space.
66 If the allocation causes stack overflow, program behavior is undefined.
67 .SH ATTRIBUTES
68 For an explanation of the terms used in this section, see
69 .BR attributes (7).
70 .ad l
71 .nh
72 .TS
73 allbox;
74 lbx lb lb
75 l l l.
76 Interface       Attribute       Value
78 .BR alloca ()
79 T}      Thread safety   MT-Safe
80 .TE
81 .hy
82 .ad
83 .sp 1
84 .SH CONFORMING TO
85 This function is not in POSIX.1.
86 .PP
87 There is evidence that the
88 .BR alloca ()
89 function appeared in 32V, PWB, PWB.2, 3BSD, and 4BSD.
90 There is a man page for it in 4.3BSD.
91 Linux uses the GNU version.
92 .SH NOTES
93 The
94 .BR alloca ()
95 function is machine- and compiler-dependent.
96 For certain applications,
97 its use can improve efficiency compared to the use of
98 .BR malloc (3)
99 plus
100 .BR free (3).
101 In certain cases,
102 it can also simplify memory deallocation in applications that use
103 .BR longjmp (3)
105 .BR siglongjmp (3).
106 Otherwise, its use is discouraged.
108 Because the space allocated by
109 .BR alloca ()
110 is allocated within the stack frame,
111 that space is automatically freed if the function return
112 is jumped over by a call to
113 .BR longjmp (3)
115 .BR siglongjmp (3).
117 The space allocated by
118 .BR alloca ()
120 .I not
121 automatically deallocated if the pointer that refers to it
122 simply goes out of scope.
124 Do not attempt to
125 .BR free (3)
126 space allocated by
127 .BR alloca ()!
128 .SS Notes on the GNU version
129 Normally,
130 .BR gcc (1)
131 translates calls to
132 .BR alloca ()
133 with inlined code.
134 This is not done when either the
135 .IR "\-ansi" ,
136 .IR "\-std=c89" ,
137 .IR "\-std=c99" ,
138 or the
139 .IR "\-std=c11"
140 option is given
141 .BR and
142 the header
143 .I <alloca.h>
144 is not included.
145 Otherwise, (without an \-ansi or \-std=c* option) the glibc version of
146 .I <stdlib.h>
147 includes
148 .I <alloca.h>
149 and that contains the lines:
151 .in +4n
153 #ifdef  __GNUC__
154 #define alloca(size)   __builtin_alloca (size)
155 #endif
159 with messy consequences if one has a private version of this function.
161 The fact that the code is inlined means that it is impossible
162 to take the address of this function, or to change its behavior
163 by linking with a different library.
165 The inlined code often consists of a single instruction adjusting
166 the stack pointer, and does not check for stack overflow.
167 Thus, there is no NULL error return.
168 .SH BUGS
169 There is no error indication if the stack frame cannot be extended.
170 (However, after a failed allocation, the program is likely to receive a
171 .B SIGSEGV
172 signal if it attempts to access the unallocated space.)
174 On many systems
175 .BR alloca ()
176 cannot be used inside the list of arguments of a function call, because
177 the stack space reserved by
178 .BR alloca ()
179 would appear on the stack in the middle of the space for the
180 function arguments.
181 .SH SEE ALSO
182 .BR brk (2),
183 .BR longjmp (3),
184 .BR malloc (3)