177 lines
7.5 KiB
HTML
177 lines
7.5 KiB
HTML
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||
|
<html>
|
||
|
<!-- Copyright (C) 1988-2016 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 5.2, http://www.gnu.org/software/texinfo/ -->
|
||
|
<head>
|
||
|
<title>Using the GNU Compiler Collection (GCC): Zero Length</title>
|
||
|
|
||
|
<meta name="description" content="Using the GNU Compiler Collection (GCC): Zero Length">
|
||
|
<meta name="keywords" content="Using the GNU Compiler Collection (GCC): Zero Length">
|
||
|
<meta name="resource-type" content="document">
|
||
|
<meta name="distribution" content="global">
|
||
|
<meta name="Generator" content="makeinfo">
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||
|
<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.smallquotation {font-size: smaller}
|
||
|
div.display {margin-left: 3.2em}
|
||
|
div.example {margin-left: 3.2em}
|
||
|
div.indentedblock {margin-left: 3.2em}
|
||
|
div.lisp {margin-left: 3.2em}
|
||
|
div.smalldisplay {margin-left: 3.2em}
|
||
|
div.smallexample {margin-left: 3.2em}
|
||
|
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
|
||
|
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.nocodebreak {white-space:nowrap}
|
||
|
span.nolinebreak {white-space:nowrap}
|
||
|
span.roman {font-family:serif; font-weight:normal}
|
||
|
span.sansserif {font-family:sans-serif; font-weight:normal}
|
||
|
ul.no-bullet {list-style: none}
|
||
|
-->
|
||
|
</style>
|
||
|
|
||
|
|
||
|
</head>
|
||
|
|
||
|
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
|
||
|
<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.17 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>Zero-length arrays are allowed in GNU C. They are very 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>In ISO C90, you would have to give <code>contents</code> a length of 1, which
|
||
|
means either you waste space or complicate the argument to <code>malloc</code>.
|
||
|
</p>
|
||
|
<p>In ISO C99, you would use a <em>flexible array member</em>, which is
|
||
|
slightly different in 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>
|