man-pages/man3/alloca.3.html
2021-03-31 01:06:50 +01:00

246 lines
5.6 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of ALLOCA</TITLE>
</HEAD><BODY>
<H1>ALLOCA</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
alloca - allocate memory that is automatically freed
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/alloca.h">alloca.h</A>&gt;</B>
<P>
<B>void *alloca(size_t </B><I>size</I><B>);</B>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>alloca</B>()
function allocates
<I>size</I>
bytes of space in the stack frame of the caller.
This temporary space is
automatically freed when the function that called
<B>alloca</B>()
returns to its caller.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
The
<B>alloca</B>()
function returns a pointer to the beginning of the allocated space.
If the allocation causes stack overflow, program behavior is undefined.
<A NAME="lbAF">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>alloca</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<A NAME="lbAG">&nbsp;</A>
<H2>CONFORMING TO</H2>
This function is not in POSIX.1.
<P>
There is evidence that the
<B>alloca</B>()
function appeared in 32V, PWB, PWB.2, 3BSD, and 4BSD.
There is a man page for it in 4.3BSD.
Linux uses the GNU version.
<A NAME="lbAH">&nbsp;</A>
<H2>NOTES</H2>
The
<B>alloca</B>()
function is machine- and compiler-dependent.
For certain applications,
its use can improve efficiency compared to the use of
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)
plus
<B><A HREF="/cgi-bin/man/man2html?3+free">free</A></B>(3).
In certain cases,
it can also simplify memory deallocation in applications that use
<B><A HREF="/cgi-bin/man/man2html?3+longjmp">longjmp</A></B>(3)
or
<B><A HREF="/cgi-bin/man/man2html?3+siglongjmp">siglongjmp</A></B>(3).
Otherwise, its use is discouraged.
<P>
Because the space allocated by
<B>alloca</B>()
is allocated within the stack frame,
that space is automatically freed if the function return
is jumped over by a call to
<B><A HREF="/cgi-bin/man/man2html?3+longjmp">longjmp</A></B>(3)
or
<B><A HREF="/cgi-bin/man/man2html?3+siglongjmp">siglongjmp</A></B>(3).
<P>
The space allocated by
<B>alloca</B>()
is
<I>not</I>
automatically deallocated if the pointer that refers to it
simply goes out of scope.
<P>
Do not attempt to
<B><A HREF="/cgi-bin/man/man2html?3+free">free</A></B>(3)
space allocated by
<B>alloca</B>()!
<A NAME="lbAI">&nbsp;</A>
<H3>Notes on the GNU version</H3>
Normally,
<B><A HREF="/cgi-bin/man/man2html?1+gcc">gcc</A></B>(1)
translates calls to
<B>alloca</B>()
with inlined code.
This is not done when either the
<I>-ansi</I>,
<I>-std=c89</I>,
<I>-std=c99</I>,
or the
<I>-std=c11</I>
option is given
<B>and</B>
the header
<I>&lt;<A HREF="file:///usr/include/alloca.h">alloca.h</A>&gt;</I>
is not included.
Otherwise, (without an -ansi or -std=c* option) the glibc version of
<I>&lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;</I>
includes
<I>&lt;<A HREF="file:///usr/include/alloca.h">alloca.h</A>&gt;</I>
and that contains the lines:
<P>
#ifdef __GNUC__
#define alloca(size) __builtin_alloca (size)
#endif
<P>
with messy consequences if one has a private version of this function.
<P>
The fact that the code is inlined means that it is impossible
to take the address of this function, or to change its behavior
by linking with a different library.
<P>
The inlined code often consists of a single instruction adjusting
the stack pointer, and does not check for stack overflow.
Thus, there is no NULL error return.
<A NAME="lbAJ">&nbsp;</A>
<H2>BUGS</H2>
There is no error indication if the stack frame cannot be extended.
(However, after a failed allocation, the program is likely to receive a
<B>SIGSEGV</B>
signal if it attempts to access the unallocated space.)
<P>
On many systems
<B>alloca</B>()
cannot be used inside the list of arguments of a function call, because
the stack space reserved by
<B>alloca</B>()
would appear on the stack in the middle of the space for the
function arguments.
<A NAME="lbAK">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+brk">brk</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+longjmp">longjmp</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3)
<A NAME="lbAL">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="4"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="5"><A HREF="#lbAF">ATTRIBUTES</A><DD>
<DT id="6"><A HREF="#lbAG">CONFORMING TO</A><DD>
<DT id="7"><A HREF="#lbAH">NOTES</A><DD>
<DL>
<DT id="8"><A HREF="#lbAI">Notes on the GNU version</A><DD>
</DL>
<DT id="9"><A HREF="#lbAJ">BUGS</A><DD>
<DT id="10"><A HREF="#lbAK">SEE ALSO</A><DD>
<DT id="11"><A HREF="#lbAL">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:35 GMT, March 31, 2021
</BODY>
</HTML>