Section (3) alloca
alloca — allocate memory that is automatically freed
of space in the stack frame of the caller. This temporary
space is automatically freed when the function that called
alloca() returns to its
returns a pointer to the beginning of the allocated space. If
the allocation causes stack overflow, program behavior is
For an explanation of the terms used in this section, see attributes(7).
This function is not in POSIX.1.
There is evidence that the
alloca() 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.
alloca() function is
machine- and compiler-dependent. For certain applications,
its use can improve efficiency compared to the use of
malloc(3) plus free(3). In certain cases,
it can also simplify memory deallocation in applications that
use longjmp(3) or siglongjmp(3). Otherwise,
its use is discouraged.
The space allocated by
alloca() is not automatically deallocated if
the pointer that refers to it simply goes out of scope.
Do not attempt to free(3) space allocated by
Notes on the GNU version
Normally, gcc(1) translates calls to
alloca() with inlined code.
This is not done when either the
−std=c99, or the
−std=c11 option is given and the header
is not included. Otherwise, (without an −ansi or
−std=c* option) the glibc version of
and that contains the lines:
#ifdef __GNUC__ #define alloca(size) __builtin_alloca (size) #endif
with messy consequences if one has a private version of this function.
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.
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.
There is no error indication if the stack frame cannot be
extended. (However, after a failed allocation, the program is
likely to receive a
signal if it attempts to access the unallocated space.)
On many systems
cannot be used inside the list of arguments of a function
call, because the stack space reserved by
alloca() would appear on the stack in the
middle of the space for the function arguments.
This page is part of release 5.04 of the Linux
man-pages project. A
description of the project, information about reporting bugs,
and the latest version of this page, can be found at
Copyright (c) 1980, 1991 Regents of the University of California.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by the University of
California, Berkeley and its contributors.
4. Neither the name of the University nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS_zsingle_quotesz__zsingle_quotesz_ AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
(#)alloca.3 5.1 (Berkeley) 5/2/91
Converted Mon Nov 29 11:05:55 1993 by Rik Faith <faithcs.unc.edu>
Modified Tue Oct 22 23:41:56 1996 by Eric S. Raymond <esrthyrsus.com>
Modified 2002-07-17, aeb
Various rewrites and additions (notes on longjmp() and SIGSEGV).
Weaken warning against use of alloca() (as per Debian bug 461100).