• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/toolchains/hndtools-armeabi-2013.11/share/doc/arm-arm-none-eabi/html/gcc/
1<html lang="en">
2<head>
3<title>Zero 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="Named-Address-Spaces.html#Named-Address-Spaces" title="Named Address Spaces">
10<link rel="next" href="Empty-Structures.html#Empty-Structures" title="Empty Structures">
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="Zero-Length"></a>
49<p>
50Next:&nbsp;<a rel="next" accesskey="n" href="Empty-Structures.html#Empty-Structures">Empty Structures</a>,
51Previous:&nbsp;<a rel="previous" accesskey="p" href="Named-Address-Spaces.html#Named-Address-Spaces">Named Address Spaces</a>,
52Up:&nbsp;<a rel="up" accesskey="u" href="C-Extensions.html#C-Extensions">C Extensions</a>
53<hr>
54</div>
55
56<h3 class="section">6.17 Arrays of Length Zero</h3>
57
58<p><a name="index-arrays-of-length-zero-2532"></a><a name="index-zero_002dlength-arrays-2533"></a><a name="index-length_002dzero-arrays-2534"></a><a name="index-flexible-array-members-2535"></a>
59Zero-length arrays are allowed in GNU C.  They are very useful as the
60last element of a structure that is really a header for a variable-length
61object:
62
63<pre class="smallexample">     struct line {
64       int length;
65       char contents[0];
66     };
67     
68     struct line *thisline = (struct line *)
69       malloc (sizeof (struct line) + this_length);
70     thisline-&gt;length = this_length;
71</pre>
72 <p>In ISO C90, you would have to give <code>contents</code> a length of 1, which
73means either you waste space or complicate the argument to <code>malloc</code>.
74
75 <p>In ISO C99, you would use a <dfn>flexible array member</dfn>, which is
76slightly different in syntax and semantics:
77
78     <ul>
79<li>Flexible array members are written as <code>contents[]</code> without
80the <code>0</code>.
81
82     <li>Flexible array members have incomplete type, and so the <code>sizeof</code>
83operator may not be applied.  As a quirk of the original implementation
84of zero-length arrays, <code>sizeof</code> evaluates to zero.
85
86     <li>Flexible array members may only appear as the last member of a
87<code>struct</code> that is otherwise non-empty.
88
89     <li>A structure containing a flexible array member, or a union containing
90such a structure (possibly recursively), may not be a member of a
91structure or an element of an array.  (However, these uses are
92permitted by GCC as extensions.) 
93</ul>
94
95 <p>GCC versions before 3.0 allowed zero-length arrays to be statically
96initialized, as if they were flexible arrays.  In addition to those
97cases that were useful, it also allowed initializations in situations
98that would corrupt later data.  Non-empty initialization of zero-length
99arrays is now treated like any case where there are more initializer
100elements than the array holds, in that a suitable warning about &ldquo;excess
101elements in array&rdquo; is given, and the excess elements (all of them, in
102this case) are ignored.
103
104 <p>Instead GCC allows static initialization of flexible array members. 
105This is equivalent to defining a new structure containing the original
106structure followed by an array of sufficient size to contain the data. 
107E.g. in the following, <code>f1</code> is constructed as if it were declared
108like <code>f2</code>.
109
110<pre class="smallexample">     struct f1 {
111       int x; int y[];
112     } f1 = { 1, { 2, 3, 4 } };
113     
114     struct f2 {
115       struct f1 f1; int data[3];
116     } f2 = { { 1 }, { 2, 3, 4 } };
117</pre>
118 <p class="noindent">The convenience of this extension is that <code>f1</code> has the desired
119type, eliminating the need to consistently refer to <code>f2.f1</code>.
120
121 <p>This has symmetry with normal static arrays, in that an array of
122unknown size is also written with <code>[]</code>.
123
124 <p>Of course, this extension only makes sense if the extra data comes at
125the end of a top-level object, as otherwise we would be overwriting
126data at subsequent offsets.  To avoid undue complication and confusion
127with initialization of deeply nested arrays, we simply disallow any
128non-empty initialization except when the structure is the top-level
129object.  For example:
130
131<pre class="smallexample">     struct foo { int x; int y[]; };
132     struct bar { struct foo z; };
133     
134     struct foo a = { 1, { 2, 3, 4 } };        // <span class="roman">Valid.</span>
135     struct bar b = { { 1, { 2, 3, 4 } } };    // <span class="roman">Invalid.</span>
136     struct bar c = { { 1, { } } };            // <span class="roman">Valid.</span>
137     struct foo d[1] = { { 1 { 2, 3, 4 } } };  // <span class="roman">Invalid.</span>
138</pre>
139 </body></html>
140
141