summaryrefslogtreecommitdiff
path: root/devdocs/gcc~13/variable-length.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/gcc~13/variable-length.html')
-rw-r--r--devdocs/gcc~13/variable-length.html29
1 files changed, 29 insertions, 0 deletions
diff --git a/devdocs/gcc~13/variable-length.html b/devdocs/gcc~13/variable-length.html
new file mode 100644
index 00000000..e7c12b28
--- /dev/null
+++ b/devdocs/gcc~13/variable-length.html
@@ -0,0 +1,29 @@
+<div class="section-level-extent" id="Variable-Length"> <div class="nav-panel"> <p> Next: <a href="variadic-macros" accesskey="n" rel="next">Macros with a Variable Number of Arguments.</a>, Previous: <a href="empty-structures" accesskey="p" rel="prev">Structures with No Members</a>, Up: <a href="c-extensions" accesskey="u" rel="up">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div> <h1 class="section" id="Arrays-of-Variable-Length"><span>6.20 Arrays of Variable Length<a class="copiable-link" href="#Arrays-of-Variable-Length"> ¶</a></span></h1> <p>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: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">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>
+</div> <p>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> <p>As an extension, GCC accepts variable-length arrays as a member of a structure or a union. For example: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">void
+foo (int n)
+{
+ struct S { int x[n]; };
+}</pre>
+</div> <p>You can use the function <code class="code">alloca</code> to get an effect much like variable-length arrays. The function <code class="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> <p>There are other differences between these two methods. Space allocated with <code class="code">alloca</code> exists until the containing <em class="emph">function</em> returns. The space for a variable-length array is deallocated as soon as the array name’s scope ends, unless you also use <code class="code">alloca</code> in this scope. </p> <p>You can also use variable-length arrays as arguments to functions: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">struct entry
+tester (int len, char data[len][len])
+{
+ /* <span class="r">…</span> */
+}</pre>
+</div> <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 class="code">sizeof</code>. </p> <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. </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">struct entry
+tester (int len; char data[len][len], int len)
+{
+ /* <span class="r">…</span> */
+}</pre>
+</div> <p>The ‘<samp class="samp">int len</samp>’ before the semicolon is a <em class="dfn">parameter forward declaration</em>, and it serves the purpose of making the name <code class="code">len</code> known when the declaration of <code class="code">data</code> is parsed. </p> <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. </p> </div> <div class="nav-panel"> <p> Next: <a href="variadic-macros">Macros with a Variable Number of Arguments.</a>, Previous: <a href="empty-structures">Structures with No Members</a>, Up: <a href="c-extensions">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution">
+ <p class="_attribution-p">
+ &copy; Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br>
+ <a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Variable-Length.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Variable-Length.html</a>
+ </p>
+</div>