You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
5.9 KiB
HTML
134 lines
5.9 KiB
HTML
4 years ago
|
<html lang="en">
|
||
|
<head>
|
||
|
<title>Variable Length - Using the GNU Compiler Collection (GCC)</title>
|
||
|
<meta http-equiv="Content-Type" content="text/html">
|
||
|
<meta name="description" content="Using the GNU Compiler Collection (GCC)">
|
||
|
<meta name="generator" content="makeinfo 4.13">
|
||
|
<link title="Top" rel="start" href="index.html#Top">
|
||
|
<link rel="up" href="C-Extensions.html#C-Extensions" title="C Extensions">
|
||
|
<link rel="prev" href="Empty-Structures.html#Empty-Structures" title="Empty Structures">
|
||
|
<link rel="next" href="Variadic-Macros.html#Variadic-Macros" title="Variadic Macros">
|
||
|
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
|
||
|
<!--
|
||
|
Copyright (C) 1988-2015 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 ``Funding Free Software'', the Front-Cover
|
||
|
Texts being (a) (see below), and with the Back-Cover Texts being (b)
|
||
|
(see below). A copy of the license is included in the section entitled
|
||
|
``GNU Free Documentation License''.
|
||
|
|
||
|
(a) The FSF's Front-Cover Text is:
|
||
|
|
||
|
A GNU Manual
|
||
|
|
||
|
(b) The FSF's Back-Cover Text is:
|
||
|
|
||
|
You have freedom to copy and modify this GNU Manual, like GNU
|
||
|
software. Copies published by the Free Software Foundation raise
|
||
|
funds for GNU development.-->
|
||
|
<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>
|
||
|
</head>
|
||
|
<body>
|
||
|
<div class="node">
|
||
|
<a name="Variable-Length"></a>
|
||
|
<p>
|
||
|
Next: <a rel="next" accesskey="n" href="Variadic-Macros.html#Variadic-Macros">Variadic Macros</a>,
|
||
|
Previous: <a rel="previous" accesskey="p" href="Empty-Structures.html#Empty-Structures">Empty Structures</a>,
|
||
|
Up: <a rel="up" accesskey="u" href="C-Extensions.html#C-Extensions">C Extensions</a>
|
||
|
<hr>
|
||
|
</div>
|
||
|
|
||
|
<h3 class="section">6.19 Arrays of Variable Length</h3>
|
||
|
|
||
|
<p><a name="index-variable_002dlength-arrays-2989"></a><a name="index-arrays-of-variable-length-2990"></a><a name="index-VLAs-2991"></a>
|
||
|
Variable-length automatic arrays are allowed in ISO C99, and as an
|
||
|
extension GCC accepts them in C90 mode and in C++. These arrays are
|
||
|
declared like any other automatic arrays, but with a length that is not
|
||
|
a constant expression. The storage is allocated at the point of
|
||
|
declaration and deallocated when the block scope containing the declaration
|
||
|
exits. For
|
||
|
example:
|
||
|
|
||
|
<pre class="smallexample"> FILE *
|
||
|
concat_fopen (char *s1, char *s2, char *mode)
|
||
|
{
|
||
|
char str[strlen (s1) + strlen (s2) + 1];
|
||
|
strcpy (str, s1);
|
||
|
strcat (str, s2);
|
||
|
return fopen (str, mode);
|
||
|
}
|
||
|
</pre>
|
||
|
<p><a name="index-scope-of-a-variable-length-array-2992"></a><a name="index-variable_002dlength-array-scope-2993"></a><a name="index-deallocating-variable-length-arrays-2994"></a>Jumping or breaking out of the scope of the array name deallocates the
|
||
|
storage. Jumping into the scope is not allowed; you get an error
|
||
|
message for it.
|
||
|
|
||
|
<p><a name="index-variable_002dlength-array-in-a-structure-2995"></a>As an extension, GCC accepts variable-length arrays as a member of
|
||
|
a structure or a union. For example:
|
||
|
|
||
|
<pre class="smallexample"> void
|
||
|
foo (int n)
|
||
|
{
|
||
|
struct S { int x[n]; };
|
||
|
}
|
||
|
</pre>
|
||
|
<p><a name="index-g_t_0040code_007balloca_007d-vs-variable_002dlength-arrays-2996"></a>You can use the function <code>alloca</code> to get an effect much like
|
||
|
variable-length arrays. The function <code>alloca</code> is available in
|
||
|
many other C implementations (but not in all). On the other hand,
|
||
|
variable-length arrays are more elegant.
|
||
|
|
||
|
<p>There are other differences between these two methods. Space allocated
|
||
|
with <code>alloca</code> exists until the containing <em>function</em> returns.
|
||
|
The space for a variable-length array is deallocated as soon as the array
|
||
|
name's scope ends. (If you use both variable-length arrays and
|
||
|
<code>alloca</code> in the same function, deallocation of a variable-length array
|
||
|
also deallocates anything more recently allocated with <code>alloca</code>.)
|
||
|
|
||
|
<p>You can also use variable-length arrays as arguments to functions:
|
||
|
|
||
|
<pre class="smallexample"> struct entry
|
||
|
tester (int len, char data[len][len])
|
||
|
{
|
||
|
/* <span class="roman">...</span> */
|
||
|
}
|
||
|
</pre>
|
||
|
<p>The length of an array is computed once when the storage is allocated
|
||
|
and is remembered for the scope of the array in case you access it with
|
||
|
<code>sizeof</code>.
|
||
|
|
||
|
<p>If you want to pass the array first and the length afterward, you can
|
||
|
use a forward declaration in the parameter list—another GNU extension.
|
||
|
|
||
|
<pre class="smallexample"> struct entry
|
||
|
tester (int len; char data[len][len], int len)
|
||
|
{
|
||
|
/* <span class="roman">...</span> */
|
||
|
}
|
||
|
</pre>
|
||
|
<p><a name="index-parameter-forward-declaration-2997"></a>The ‘<samp><span class="samp">int len</span></samp>’ before the semicolon is a <dfn>parameter forward
|
||
|
declaration</dfn>, and it serves the purpose of making the name <code>len</code>
|
||
|
known when the declaration of <code>data</code> is parsed.
|
||
|
|
||
|
<p>You can write any number of such parameter forward declarations in the
|
||
|
parameter list. They can be separated by commas or semicolons, but the
|
||
|
last one must end with a semicolon, which is followed by the “real”
|
||
|
parameter declarations. Each forward declaration must match a “real”
|
||
|
declaration in parameter name and data type. ISO C99 does not support
|
||
|
parameter forward declarations.
|
||
|
|
||
|
</body></html>
|
||
|
|