maint: update copyright
[gnulib.git] / doc / alloca.texi
1 @c Documentation of gnulib module 'alloca'.
2
3 @c Copyright (C) 2004, 2007, 2009-2014 Free Software Foundation, Inc.
4
5 @c Permission is granted to copy, distribute and/or modify this document
6 @c under the terms of the GNU Free Documentation License, Version 1.3 or
7 @c any later version published by the Free Software Foundation; with no
8 @c Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
9 @c Texts.  A copy of the license is included in the ``GNU Free
10 @c Documentation License'' file as part of this distribution.
11
12 The alloca module provides for a function @code{alloca} which allocates
13 memory on the stack, where the system allows it. A memory block allocated with
14 @code{alloca} exists only until the function that calls @code{alloca} returns
15 or exits abruptly.
16
17 There are a few systems where this is not possible: HP-UX systems, and some
18 other platforms when the C++ compiler is used. On these platforms the alloca
19 module provides a @code{malloc} based emulation. This emulation will not free a
20 memory block immediately when the calling function returns, but rather will
21 wait until the next @code{alloca} call from a function with the same or a
22 shorter stack length. Thus, in some cases, a few memory blocks will be kept
23 although they are not needed any more.
24
25 The user can @code{#include <alloca.h>} and use @code{alloca} on all platforms.
26 Note that the @code{#include <alloca.h>} must be the first one after the
27 autoconf-generated @file{config.h}, for AIX 3 compatibility. Thanks to IBM for
28 this nice restriction!
29
30 Note that GCC 3.1 and 3.2 can @emph{inline} functions that call @code{alloca}.
31 When this happens, the memory blocks allocated with @code{alloca} will not be
32 freed until @emph{the end of the calling function}. If this calling function
33 runs a loop calling the function that uses @code{alloca}, the program easily
34 gets a stack overflow and crashes. To protect against this compiler behaviour,
35 you can mark the function that uses @code{alloca} with the following attribute:
36
37 @smallexample
38 #ifdef __GNUC__
39 __attribute__ ((__noinline__))
40 #endif
41 @end smallexample
42
43 An alternative to this module is the @samp{alloca-opt} module.