summaryrefslogtreecommitdiffstats
path: root/upstream/opensuse-leap-15-6/man3/alloca.3
blob: bcd917da66e194b3616aa24b5ee1fa9515371bf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'\" t
.\" Copyright (c) 1980, 1991 Regents of the University of California.
.\" All rights reserved.
.\"
.\" SPDX-License-Identifier: BSD-4-Clause-UC
.\"
.\"     @(#)alloca.3	5.1 (Berkeley) 5/2/91
.\"
.\" Converted Mon Nov 29 11:05:55 1993 by Rik Faith <faith@cs.unc.edu>
.\" Modified Tue Oct 22 23:41:56 1996 by Eric S. Raymond <esr@thyrsus.com>
.\" Modified 2002-07-17, aeb
.\" 2008-01-24, mtk:
.\"     Various rewrites and additions (notes on longjmp() and SIGSEGV).
.\"     Weaken warning against use of alloca() (as per Debian bug 461100).
.\"
.TH alloca 3 2023-03-30 "Linux man-pages 6.04"
.SH NAME
alloca \- allocate memory that is automatically freed
.SH LIBRARY
Standard C library
.RI ( libc ", " \-lc )
.SH SYNOPSIS
.nf
.B #include <alloca.h>
.PP
.BI "void *alloca(size_t " size );
.fi
.SH DESCRIPTION
The
.BR alloca ()
function allocates
.I size
bytes of space in the stack frame of the caller.
This temporary space is
automatically freed when the function that called
.BR alloca ()
returns to its caller.
.SH RETURN VALUE
The
.BR alloca ()
function returns a pointer to the beginning of the allocated space.
If the allocation causes stack overflow, program behavior is undefined.
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.ad l
.nh
.TS
allbox;
lbx lb lb
l l l.
Interface	Attribute	Value
T{
.BR alloca ()
T}	Thread safety	MT-Safe
.TE
.hy
.ad
.sp 1
.SH STANDARDS
None.
.SH HISTORY
PWB, 32V.
.SH NOTES
The
.BR alloca ()
function is machine- and compiler-dependent.
Because it allocates from the stack, it's faster than
.BR malloc (3)
and
.BR free (3).
In certain cases,
it can also simplify memory deallocation in applications that use
.BR longjmp (3)
or
.BR siglongjmp (3).
Otherwise, its use is discouraged.
.PP
Because the space allocated by
.BR alloca ()
is allocated within the stack frame,
that space is automatically freed if the function return
is jumped over by a call to
.BR longjmp (3)
or
.BR siglongjmp (3).
.PP
The space allocated by
.BR alloca ()
is
.I not
automatically deallocated if the pointer that refers to it
simply goes out of scope.
.PP
Do not attempt to
.BR free (3)
space allocated by
.BR alloca ()!
.PP
By necessity,
.BR alloca ()
is a compiler built-in, also known as
.BR __builtin_alloca ().
By default, modern compilers automatically translate all uses of
.BR alloca ()
into the built-in, but this is forbidden if standards conformance is requested
.RI ( "\-ansi" ,
.IR "\-std=c*" ),
in which case
.I <alloca.h>
is required, lest a symbol dependency be emitted.
.PP
The fact that
.BR alloca ()
is a built-in means it is impossible to take its address
or to change its behavior by linking with a different library.
.PP
Variable length arrays (VLAs) are part of the C99 standard,
optional since C11, and can be used for a similar purpose.
However, they do not port to standard C++, and, being variables,
live in their block scope and don't have an allocator-like interface,
making them unfit for implementing functionality like
.BR strdupa (3).
.SH BUGS
Due to the nature of the stack, it is impossible to check if the allocation
would overflow the space available, and, hence, neither is indicating an error.
(However, the program is likely to receive a
.B SIGSEGV
signal if it attempts to access unavailable space.)
.PP
On many systems
.BR alloca ()
cannot be used inside the list of arguments of a function call, because
the stack space reserved by
.BR alloca ()
would appear on the stack in the middle of the space for the
function arguments.
.SH SEE ALSO
.BR brk (2),
.BR longjmp (3),
.BR malloc (3)