• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/toolchains/hndtools-armeabi-2011.09/share/doc/arm-arm-none-eabi/html/gcc/
1<html lang="en">
2<head>
3<title>Variable Length - Using the GNU Compiler Collection (GCC)</title>
4<meta http-equiv="Content-Type" content="text/html">
5<meta name="description" content="Using the GNU Compiler Collection (GCC)">
6<meta name="generator" content="makeinfo 4.13">
7<link title="Top" rel="start" href="index.html#Top">
8<link rel="up" href="C-Extensions.html#C-Extensions" title="C Extensions">
9<link rel="prev" href="Zero-Length.html#Zero-Length" title="Zero Length">
10<link rel="next" href="Empty-Structures.html#Empty-Structures" title="Empty Structures">
11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
12<!--
13Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997,
141998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
152010 Free Software Foundation, Inc.
16
17Permission is granted to copy, distribute and/or modify this document
18under the terms of the GNU Free Documentation License, Version 1.3 or
19any later version published by the Free Software Foundation; with the
20Invariant Sections being ``Funding Free Software'', the Front-Cover
21Texts being (a) (see below), and with the Back-Cover Texts being (b)
22(see below).  A copy of the license is included in the section entitled
23``GNU Free Documentation License''.
24
25(a) The FSF's Front-Cover Text is:
26
27     A GNU Manual
28
29(b) The FSF's Back-Cover Text is:
30
31     You have freedom to copy and modify this GNU Manual, like GNU
32     software.  Copies published by the Free Software Foundation raise
33     funds for GNU development.-->
34<meta http-equiv="Content-Style-Type" content="text/css">
35<style type="text/css"><!--
36  pre.display { font-family:inherit }
37  pre.format  { font-family:inherit }
38  pre.smalldisplay { font-family:inherit; font-size:smaller }
39  pre.smallformat  { font-family:inherit; font-size:smaller }
40  pre.smallexample { font-size:smaller }
41  pre.smalllisp    { font-size:smaller }
42  span.sc    { font-variant:small-caps }
43  span.roman { font-family:serif; font-weight:normal; } 
44  span.sansserif { font-family:sans-serif; font-weight:normal; } 
45--></style>
46<link rel="stylesheet" type="text/css" href="../cs.css">
47</head>
48<body>
49<div class="node">
50<a name="Variable-Length"></a>
51<p>
52Next:&nbsp;<a rel="next" accesskey="n" href="Empty-Structures.html#Empty-Structures">Empty Structures</a>,
53Previous:&nbsp;<a rel="previous" accesskey="p" href="Zero-Length.html#Zero-Length">Zero Length</a>,
54Up:&nbsp;<a rel="up" accesskey="u" href="C-Extensions.html#C-Extensions">C Extensions</a>
55<hr>
56</div>
57
58<h3 class="section">6.19 Arrays of Variable Length</h3>
59
60<p><a name="index-variable_002dlength-arrays-2350"></a><a name="index-arrays-of-variable-length-2351"></a><a name="index-VLAs-2352"></a>
61Variable-length automatic arrays are allowed in ISO C99, and as an
62extension GCC accepts them in C90 mode and in C++.  These arrays are
63declared like any other automatic arrays, but with a length that is not
64a constant expression.  The storage is allocated at the point of
65declaration and deallocated when the brace-level is exited.  For
66example:
67
68<pre class="smallexample">     FILE *
69     concat_fopen (char *s1, char *s2, char *mode)
70     {
71       char str[strlen (s1) + strlen (s2) + 1];
72       strcpy (str, s1);
73       strcat (str, s2);
74       return fopen (str, mode);
75     }
76</pre>
77 <p><a name="index-scope-of-a-variable-length-array-2353"></a><a name="index-variable_002dlength-array-scope-2354"></a><a name="index-deallocating-variable-length-arrays-2355"></a>Jumping or breaking out of the scope of the array name deallocates the
78storage.  Jumping into the scope is not allowed; you get an error
79message for it.
80
81 <p><a name="index-g_t_0040code_007balloca_007d-vs-variable_002dlength-arrays-2356"></a>You can use the function <code>alloca</code> to get an effect much like
82variable-length arrays.  The function <code>alloca</code> is available in
83many other C implementations (but not in all).  On the other hand,
84variable-length arrays are more elegant.
85
86 <p>There are other differences between these two methods.  Space allocated
87with <code>alloca</code> exists until the containing <em>function</em> returns. 
88The space for a variable-length array is deallocated as soon as the array
89name's scope ends.  (If you use both variable-length arrays and
90<code>alloca</code> in the same function, deallocation of a variable-length array
91will also deallocate anything more recently allocated with <code>alloca</code>.)
92
93 <p>You can also use variable-length arrays as arguments to functions:
94
95<pre class="smallexample">     struct entry
96     tester (int len, char data[len][len])
97     {
98       /* <span class="roman">...</span> */
99     }
100</pre>
101 <p>The length of an array is computed once when the storage is allocated
102and is remembered for the scope of the array in case you access it with
103<code>sizeof</code>.
104
105 <p>If you want to pass the array first and the length afterward, you can
106use a forward declaration in the parameter list&mdash;another GNU extension.
107
108<pre class="smallexample">     struct entry
109     tester (int len; char data[len][len], int len)
110     {
111       /* <span class="roman">...</span> */
112     }
113</pre>
114 <p><a name="index-parameter-forward-declaration-2357"></a>The &lsquo;<samp><span class="samp">int len</span></samp>&rsquo; before the semicolon is a <dfn>parameter forward
115declaration</dfn>, and it serves the purpose of making the name <code>len</code>
116known when the declaration of <code>data</code> is parsed.
117
118 <p>You can write any number of such parameter forward declarations in the
119parameter list.  They can be separated by commas or semicolons, but the
120last one must end with a semicolon, which is followed by the &ldquo;real&rdquo;
121parameter declarations.  Each forward declaration must match a &ldquo;real&rdquo;
122declaration in parameter name and data type.  ISO C99 does not support
123parameter forward declarations.
124
125 </body></html>
126
127