|
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <!-- Copyright (C) 1988-2020 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. -->
- <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Zero Length (Using the GNU Compiler Collection (GCC))</title>
-
- <meta name="description" content="Zero Length (Using the GNU Compiler Collection (GCC))">
- <meta name="keywords" content="Zero Length (Using the GNU Compiler Collection (GCC))">
- <meta name="resource-type" content="document">
- <meta name="distribution" content="global">
- <meta name="Generator" content="makeinfo">
- <link href="index.html#Top" rel="start" title="Top">
- <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
- <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
- <link href="C-Extensions.html#C-Extensions" rel="up" title="C Extensions">
- <link href="Empty-Structures.html#Empty-Structures" rel="next" title="Empty Structures">
- <link href="Named-Address-Spaces.html#Named-Address-Spaces" rel="prev" title="Named Address Spaces">
- <style type="text/css">
- <!--
- a.summary-letter {text-decoration: none}
- blockquote.indentedblock {margin-right: 0em}
- blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
- blockquote.smallquotation {font-size: smaller}
- div.display {margin-left: 3.2em}
- div.example {margin-left: 3.2em}
- div.lisp {margin-left: 3.2em}
- div.smalldisplay {margin-left: 3.2em}
- div.smallexample {margin-left: 3.2em}
- div.smalllisp {margin-left: 3.2em}
- kbd {font-style: oblique}
- pre.display {font-family: inherit}
- pre.format {font-family: inherit}
- pre.menu-comment {font-family: serif}
- pre.menu-preformatted {font-family: serif}
- pre.smalldisplay {font-family: inherit; font-size: smaller}
- pre.smallexample {font-size: smaller}
- pre.smallformat {font-family: inherit; font-size: smaller}
- pre.smalllisp {font-size: smaller}
- span.nolinebreak {white-space: nowrap}
- span.roman {font-family: initial; font-weight: normal}
- span.sansserif {font-family: sans-serif; font-weight: normal}
- ul.no-bullet {list-style: none}
- -->
- </style>
-
-
- </head>
-
- <body lang="en">
- <a name="Zero-Length"></a>
- <div class="header">
- <p>
- Next: <a href="Empty-Structures.html#Empty-Structures" accesskey="n" rel="next">Empty Structures</a>, Previous: <a href="Named-Address-Spaces.html#Named-Address-Spaces" accesskey="p" rel="prev">Named Address Spaces</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
- </div>
- <hr>
- <a name="Arrays-of-Length-Zero"></a>
- <h3 class="section">6.18 Arrays of Length Zero</h3>
- <a name="index-arrays-of-length-zero"></a>
- <a name="index-zero_002dlength-arrays"></a>
- <a name="index-length_002dzero-arrays"></a>
- <a name="index-flexible-array-members"></a>
-
- <p>Declaring zero-length arrays is allowed in GNU C as an extension.
- A zero-length array can be useful as the last element of a structure
- that is really a header for a variable-length object:
- </p>
- <div class="smallexample">
- <pre class="smallexample">struct line {
- int length;
- char contents[0];
- };
-
- struct line *thisline = (struct line *)
- malloc (sizeof (struct line) + this_length);
- thisline->length = this_length;
- </pre></div>
-
- <p>Although the size of a zero-length array is zero, an array member of
- this kind may increase the size of the enclosing type as a result of tail
- padding. The offset of a zero-length array member from the beginning
- of the enclosing structure is the same as the offset of an array with
- one or more elements of the same type. The alignment of a zero-length
- array is the same as the alignment of its elements.
- </p>
- <p>Declaring zero-length arrays in other contexts, including as interior
- members of structure objects or as non-member objects, is discouraged.
- Accessing elements of zero-length arrays declared in such contexts is
- undefined and may be diagnosed.
- </p>
- <p>In the absence of the zero-length array extension, in ISO C90
- the <code>contents</code> array in the example above would typically be declared
- to have a single element. Unlike a zero-length array which only contributes
- to the size of the enclosing structure for the purposes of alignment,
- a one-element array always occupies at least as much space as a single
- object of the type. Although using one-element arrays this way is
- discouraged, GCC handles accesses to trailing one-element array members
- analogously to zero-length arrays.
- </p>
- <p>The preferred mechanism to declare variable-length types like
- <code>struct line</code> above is the ISO C99 <em>flexible array member</em>,
- with slightly different syntax and semantics:
- </p>
- <ul>
- <li> Flexible array members are written as <code>contents[]</code> without
- the <code>0</code>.
-
- </li><li> Flexible array members have incomplete type, and so the <code>sizeof</code>
- operator may not be applied. As a quirk of the original implementation
- of zero-length arrays, <code>sizeof</code> evaluates to zero.
-
- </li><li> Flexible array members may only appear as the last member of a
- <code>struct</code> that is otherwise non-empty.
-
- </li><li> A structure containing a flexible array member, or a union containing
- such a structure (possibly recursively), may not be a member of a
- structure or an element of an array. (However, these uses are
- permitted by GCC as extensions.)
- </li></ul>
-
- <p>Non-empty initialization of zero-length
- arrays is treated like any case where there are more initializer
- elements than the array holds, in that a suitable warning about “excess
- elements in array” is given, and the excess elements (all of them, in
- this case) are ignored.
- </p>
- <p>GCC allows static initialization of flexible array members.
- This is equivalent to defining a new structure containing the original
- structure followed by an array of sufficient size to contain the data.
- E.g. in the following, <code>f1</code> is constructed as if it were declared
- like <code>f2</code>.
- </p>
- <div class="smallexample">
- <pre class="smallexample">struct f1 {
- int x; int y[];
- } f1 = { 1, { 2, 3, 4 } };
-
- struct f2 {
- struct f1 f1; int data[3];
- } f2 = { { 1 }, { 2, 3, 4 } };
- </pre></div>
-
- <p>The convenience of this extension is that <code>f1</code> has the desired
- type, eliminating the need to consistently refer to <code>f2.f1</code>.
- </p>
- <p>This has symmetry with normal static arrays, in that an array of
- unknown size is also written with <code>[]</code>.
- </p>
- <p>Of course, this extension only makes sense if the extra data comes at
- the end of a top-level object, as otherwise we would be overwriting
- data at subsequent offsets. To avoid undue complication and confusion
- with initialization of deeply nested arrays, we simply disallow any
- non-empty initialization except when the structure is the top-level
- object. For example:
- </p>
- <div class="smallexample">
- <pre class="smallexample">struct foo { int x; int y[]; };
- struct bar { struct foo z; };
-
- struct foo a = { 1, { 2, 3, 4 } }; // <span class="roman">Valid.</span>
- struct bar b = { { 1, { 2, 3, 4 } } }; // <span class="roman">Invalid.</span>
- struct bar c = { { 1, { } } }; // <span class="roman">Valid.</span>
- struct foo d[1] = { { 1, { 2, 3, 4 } } }; // <span class="roman">Invalid.</span>
- </pre></div>
-
- <hr>
- <div class="header">
- <p>
- Next: <a href="Empty-Structures.html#Empty-Structures" accesskey="n" rel="next">Empty Structures</a>, Previous: <a href="Named-Address-Spaces.html#Named-Address-Spaces" accesskey="p" rel="prev">Named Address Spaces</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
- </div>
-
-
-
- </body>
- </html>
|