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="Empty-Structures.html#Empty-Structures" title="Empty Structures"> 10<link rel="next" href="Variadic-Macros.html#Variadic-Macros" title="Variadic Macros"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988-2013 Free Software Foundation, Inc. 14 15Permission is granted to copy, distribute and/or modify this document 16under the terms of the GNU Free Documentation License, Version 1.3 or 17any later version published by the Free Software Foundation; with the 18Invariant Sections being ``Funding Free Software'', the Front-Cover 19Texts being (a) (see below), and with the Back-Cover Texts being (b) 20(see below). A copy of the license is included in the section entitled 21``GNU Free Documentation License''. 22 23(a) The FSF's Front-Cover Text is: 24 25 A GNU Manual 26 27(b) The FSF's Back-Cover Text is: 28 29 You have freedom to copy and modify this GNU Manual, like GNU 30 software. Copies published by the Free Software Foundation raise 31 funds for GNU development.--> 32<meta http-equiv="Content-Style-Type" content="text/css"> 33<style type="text/css"><!-- 34 pre.display { font-family:inherit } 35 pre.format { font-family:inherit } 36 pre.smalldisplay { font-family:inherit; font-size:smaller } 37 pre.smallformat { font-family:inherit; font-size:smaller } 38 pre.smallexample { font-size:smaller } 39 pre.smalllisp { font-size:smaller } 40 span.sc { font-variant:small-caps } 41 span.roman { font-family:serif; font-weight:normal; } 42 span.sansserif { font-family:sans-serif; font-weight:normal; } 43--></style> 44<link rel="stylesheet" type="text/css" href="../cs.css"> 45</head> 46<body> 47<div class="node"> 48<a name="Variable-Length"></a> 49<p> 50Next: <a rel="next" accesskey="n" href="Variadic-Macros.html#Variadic-Macros">Variadic Macros</a>, 51Previous: <a rel="previous" accesskey="p" href="Empty-Structures.html#Empty-Structures">Empty Structures</a>, 52Up: <a rel="up" accesskey="u" href="C-Extensions.html#C-Extensions">C Extensions</a> 53<hr> 54</div> 55 56<h3 class="section">6.19 Arrays of Variable Length</h3> 57 58<p><a name="index-variable_002dlength-arrays-2538"></a><a name="index-arrays-of-variable-length-2539"></a><a name="index-VLAs-2540"></a> 59Variable-length automatic arrays are allowed in ISO C99, and as an 60extension GCC accepts them in C90 mode and in C++. These arrays are 61declared like any other automatic arrays, but with a length that is not 62a constant expression. The storage is allocated at the point of 63declaration and deallocated when the block scope containing the declaration 64exits. For 65example: 66 67<pre class="smallexample"> FILE * 68 concat_fopen (char *s1, char *s2, char *mode) 69 { 70 char str[strlen (s1) + strlen (s2) + 1]; 71 strcpy (str, s1); 72 strcat (str, s2); 73 return fopen (str, mode); 74 } 75</pre> 76 <p><a name="index-scope-of-a-variable-length-array-2541"></a><a name="index-variable_002dlength-array-scope-2542"></a><a name="index-deallocating-variable-length-arrays-2543"></a>Jumping or breaking out of the scope of the array name deallocates the 77storage. Jumping into the scope is not allowed; you get an error 78message for it. 79 80 <p><a name="index-g_t_0040code_007balloca_007d-vs-variable_002dlength-arrays-2544"></a>You can use the function <code>alloca</code> to get an effect much like 81variable-length arrays. The function <code>alloca</code> is available in 82many other C implementations (but not in all). On the other hand, 83variable-length arrays are more elegant. 84 85 <p>There are other differences between these two methods. Space allocated 86with <code>alloca</code> exists until the containing <em>function</em> returns. 87The space for a variable-length array is deallocated as soon as the array 88name's scope ends. (If you use both variable-length arrays and 89<code>alloca</code> in the same function, deallocation of a variable-length array 90also deallocates anything more recently allocated with <code>alloca</code>.) 91 92 <p>You can also use variable-length arrays as arguments to functions: 93 94<pre class="smallexample"> struct entry 95 tester (int len, char data[len][len]) 96 { 97 /* <span class="roman">...</span> */ 98 } 99</pre> 100 <p>The length of an array is computed once when the storage is allocated 101and is remembered for the scope of the array in case you access it with 102<code>sizeof</code>. 103 104 <p>If you want to pass the array first and the length afterward, you can 105use a forward declaration in the parameter list—another GNU extension. 106 107<pre class="smallexample"> struct entry 108 tester (int len; char data[len][len], int len) 109 { 110 /* <span class="roman">...</span> */ 111 } 112</pre> 113 <p><a name="index-parameter-forward-declaration-2545"></a>The ‘<samp><span class="samp">int len</span></samp>’ before the semicolon is a <dfn>parameter forward 114declaration</dfn>, and it serves the purpose of making the name <code>len</code> 115known when the declaration of <code>data</code> is parsed. 116 117 <p>You can write any number of such parameter forward declarations in the 118parameter list. They can be separated by commas or semicolons, but the 119last one must end with a semicolon, which is followed by the “real” 120parameter declarations. Each forward declaration must match a “real” 121declaration in parameter name and data type. ISO C99 does not support 122parameter forward declarations. 123 124 </body></html> 125 126