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