c-strtof, c-strtod, c-strtold: Make multithread-safe.
[gnulib.git] / doc / alloca-opt.texi
blobaf2777464a4599463041daacaf26b953f1948a49
1 @c Documentation of gnulib module 'alloca-opt'.
3 @c Copyright (C) 2004, 2007, 2009--2024 Free Software Foundation, Inc.
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, no Front-Cover Texts, and no Back-Cover Texts.  A
9 @c copy of the license is at <https://www.gnu.org/licenses/fdl-1.3.en.html>.
11 The @code{alloca-opt} module provides for a function @code{alloca} which allocates
12 memory on the stack, where the system allows it. A memory block allocated with
13 @code{alloca} exists only until the function that calls @code{alloca} returns
14 or exits abruptly.
16 There are a few systems where this is not possible: HP-UX systems, and some
17 other platforms when the C++ compiler is used. On these platforms the
18 @code{alloca-opt} module provides no replacement, just a preprocessor macro
19 HAVE_ALLOCA.
21 The user can @code{#include <alloca.h>} on all platforms, and use
22 @code{alloca} on those platforms where the preprocessor macro HAVE_ALLOCA
23 evaluates to true. If HAVE_ALLOCA is false, the code should use a heap-based
24 memory allocation based on @code{malloc} or (in C++) @code{new}. Note that
25 the @code{#include <alloca.h>} must be the first one after the
26 autoconf-generated @file{config.h}, for AIX 3 compatibility. Thanks to IBM for
27 this nice restriction!
29 Note that GCC 3.1 and 3.2 can @emph{inline} functions that call @code{alloca}.
30 When this happens, the memory blocks allocated with @code{alloca} will not be
31 freed until @emph{the end of the calling function}. If this calling function
32 runs a loop calling the function that uses @code{alloca}, the program easily
33 gets a stack overflow and crashes. To protect against this compiler behaviour,
34 you can mark the function that uses @code{alloca} with the following attribute:
36 @smallexample
37 #ifdef __GNUC__
38 __attribute__ ((__noinline__))
39 #endif
40 @end smallexample