| <html lang="en"> |
| <head> |
| <title>Variable Size Automatic - The GNU C Library</title> |
| <meta http-equiv="Content-Type" content="text/html"> |
| <meta name="description" content="The GNU C Library"> |
| <meta name="generator" content="makeinfo 4.13"> |
| <link title="Top" rel="start" href="index.html#Top"> |
| <link rel="up" href="Memory-Allocation.html#Memory-Allocation" title="Memory Allocation"> |
| <link rel="prev" href="Obstacks.html#Obstacks" title="Obstacks"> |
| <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> |
| <!-- |
| This file documents the GNU C library. |
| |
| This is Edition 0.12, last updated 2007-10-27, |
| of `The GNU C Library Reference Manual', for version |
| 2.8 (Sourcery G++ Lite 2011.03-41). |
| |
| Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002, |
| 2003, 2007, 2008, 2010 Free Software Foundation, Inc. |
| |
| Permission is granted to copy, distribute and/or modify this document |
| under the terms of the GNU Free Documentation License, Version 1.3 or |
| any later version published by the Free Software Foundation; with the |
| Invariant Sections being ``Free Software Needs Free Documentation'' |
| and ``GNU Lesser General Public License'', the Front-Cover texts being |
| ``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A |
| copy of the license is included in the section entitled "GNU Free |
| Documentation License". |
| |
| (a) The FSF's Back-Cover Text is: ``You have the freedom to |
| copy and modify this GNU manual. Buying copies from the FSF |
| supports it in developing GNU and promoting software freedom.''--> |
| <meta http-equiv="Content-Style-Type" content="text/css"> |
| <style type="text/css"><!-- |
| pre.display { font-family:inherit } |
| pre.format { font-family:inherit } |
| pre.smalldisplay { font-family:inherit; font-size:smaller } |
| pre.smallformat { font-family:inherit; font-size:smaller } |
| pre.smallexample { font-size:smaller } |
| pre.smalllisp { font-size:smaller } |
| span.sc { font-variant:small-caps } |
| span.roman { font-family:serif; font-weight:normal; } |
| span.sansserif { font-family:sans-serif; font-weight:normal; } |
| --></style> |
| <link rel="stylesheet" type="text/css" href="../cs.css"> |
| </head> |
| <body> |
| <div class="node"> |
| <a name="Variable-Size-Automatic"></a> |
| <p> |
| Previous: <a rel="previous" accesskey="p" href="Obstacks.html#Obstacks">Obstacks</a>, |
| Up: <a rel="up" accesskey="u" href="Memory-Allocation.html#Memory-Allocation">Memory Allocation</a> |
| <hr> |
| </div> |
| |
| <h4 class="subsection">3.2.5 Automatic Storage with Variable Size</h4> |
| |
| <p><a name="index-automatic-freeing-341"></a><a name="index-g_t_0040code_007balloca_007d-function-342"></a><a name="index-automatic-storage-with-variable-size-343"></a> |
| The function <code>alloca</code> supports a kind of half-dynamic allocation in |
| which blocks are allocated dynamically but freed automatically. |
| |
| <p>Allocating a block with <code>alloca</code> is an explicit action; you can |
| allocate as many blocks as you wish, and compute the size at run time. But |
| all the blocks are freed when you exit the function that <code>alloca</code> was |
| called from, just as if they were automatic variables declared in that |
| function. There is no way to free the space explicitly. |
| |
| <p>The prototype for <code>alloca</code> is in <samp><span class="file">stdlib.h</span></samp>. This function is |
| a BSD extension. |
| <a name="index-stdlib_002eh-344"></a> |
| <!-- stdlib.h --> |
| <!-- GNU, BSD --> |
| |
| <div class="defun"> |
| — Function: void * <b>alloca</b> (<var>size_t size</var>)<var>;<a name="index-alloca-345"></a></var><br> |
| <blockquote><p>The return value of <code>alloca</code> is the address of a block of <var>size</var> |
| bytes of memory, allocated in the stack frame of the calling function. |
| </p></blockquote></div> |
| |
| <p>Do not use <code>alloca</code> inside the arguments of a function call—you |
| will get unpredictable results, because the stack space for the |
| <code>alloca</code> would appear on the stack in the middle of the space for |
| the function arguments. An example of what to avoid is <code>foo (x, |
| alloca (4), y)</code>. |
| <!-- This might get fixed in future versions of GCC, but that won't make --> |
| <!-- it safe with compilers generally. --> |
| |
| <ul class="menu"> |
| <li><a accesskey="1" href="Alloca-Example.html#Alloca-Example">Alloca Example</a>: Example of using <code>alloca</code>. |
| <li><a accesskey="2" href="Advantages-of-Alloca.html#Advantages-of-Alloca">Advantages of Alloca</a>: Reasons to use <code>alloca</code>. |
| <li><a accesskey="3" href="Disadvantages-of-Alloca.html#Disadvantages-of-Alloca">Disadvantages of Alloca</a>: Reasons to avoid <code>alloca</code>. |
| <li><a accesskey="4" href="GNU-C-Variable_002dSize-Arrays.html#GNU-C-Variable_002dSize-Arrays">GNU C Variable-Size Arrays</a>: Only in GNU C, here is an alternative |
| method of allocating dynamically and |
| freeing automatically. |
| </ul> |
| |
| </body></html> |
| |