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
|
<table class="head">
<tr>
<td class="head-ltitle">ALLOCA(3)</td>
<td class="head-vol">Library Functions Manual</td>
<td class="head-rtitle">ALLOCA(3)</td>
</tr>
</table>
<div class="manual-text">
<section class="Sh">
<h1 class="Sh" id="NAME"><a class="permalink" href="#NAME">NAME</a></h1>
<p class="Pp"><code class="Nm">alloca</code> — <span class="Nd">memory
allocator</span></p>
</section>
<section class="Sh">
<h1 class="Sh" id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">SYNOPSIS</a></h1>
<p class="Pp"><code class="In">#include
<<a class="In">stdlib.h</a>></code></p>
<p class="Pp"><var class="Ft">void *</var>
<br/>
<code class="Fn">alloca</code>(<var class="Fa" style="white-space: nowrap;">size_t
size</var>);</p>
</section>
<section class="Sh">
<h1 class="Sh" id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">DESCRIPTION</a></h1>
<p class="Pp">The
<a class="permalink" href="#alloca"><code class="Fn" id="alloca">alloca</code></a>()
function or macro allocates <var class="Fa">size</var> bytes of space in the
stack frame of the caller. This temporary space is automatically freed on
return.</p>
</section>
<section class="Sh">
<h1 class="Sh" id="RETURN_VALUES"><a class="permalink" href="#RETURN_VALUES">RETURN
VALUES</a></h1>
<p class="Pp"><code class="Fn">alloca</code>() returns a pointer to the
beginning of the allocated space.</p>
</section>
<section class="Sh">
<h1 class="Sh" id="SEE_ALSO"><a class="permalink" href="#SEE_ALSO">SEE
ALSO</a></h1>
<p class="Pp"><a class="Xr">brk(2)</a>, <a class="Xr">calloc(3)</a>,
<a class="Xr">getpagesize(3)</a>, <a class="Xr">malloc(3)</a>,
<a class="Xr">realloc(3)</a></p>
</section>
<section class="Sh">
<h1 class="Sh" id="HISTORY"><a class="permalink" href="#HISTORY">HISTORY</a></h1>
<p class="Pp"><code class="Fn">alloca</code>() appeared in
<span class="Ux">Version 7 AT&T UNIX/32V</span>.</p>
</section>
<section class="Sh">
<h1 class="Sh" id="BUGS"><a class="permalink" href="#BUGS">BUGS</a></h1>
<p class="Pp"><code class="Fn">alloca</code>() is machine and compiler
dependent; its use is discouraged.</p>
<p class="Pp"><code class="Fn">alloca</code>() is slightly unsafe because it
cannot ensure that the pointer returned points to a valid and usable block
of memory. The allocation made may exceed the bounds of the stack, or even
go further into other objects in memory, and
<code class="Fn">alloca</code>() cannot determine such an error. Avoid
<code class="Fn">alloca</code>() with large unbounded allocations.</p>
<p class="Pp">The use of C99 variable-length arrays and
<code class="Fn">alloca</code>() in the same function will cause the
lifetime of <code class="Fn">alloca</code>()'s storage to be limited to the
block containing the <code class="Fn">alloca</code>(). For example, in the
following snippet, <var class="Va">p</var>'s lifetime does not extend
outside of the block, whereas it would've if <var class="Va">vla</var>
hadn't been defined or had been defined as a fixed-length array:</p>
<div class="Bd Pp Bd-indent Li">
<pre>char *p;
{
const int n = 100;
int vla[n];
p = alloca(32);
strcpy(p, "Hello, world!");
printf("Inside: %s\n", p); /* Valid. */
}
printf("Outside: %s\n", p); /* Undefined. */</pre>
</div>
</section>
</div>
<table class="foot">
<tr>
<td class="foot-date">February 19, 2026</td>
<td class="foot-os">FreeBSD 15.0</td>
</tr>
</table>
|