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="Variable-Length.html#Variable-Length" title="Variable Length"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 141998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 152010 Free Software Foundation, Inc. 16 17Permission is granted to copy, distribute and/or modify this document 18under the terms of the GNU Free Documentation License, Version 1.3 or 19any later version published by the Free Software Foundation; with the 20Invariant Sections being ``Funding Free Software'', the Front-Cover 21Texts being (a) (see below), and with the Back-Cover Texts being (b) 22(see below). A copy of the license is included in the section entitled 23``GNU Free Documentation License''. 24 25(a) The FSF's Front-Cover Text is: 26 27 A GNU Manual 28 29(b) The FSF's Back-Cover Text is: 30 31 You have freedom to copy and modify this GNU Manual, like GNU 32 software. Copies published by the Free Software Foundation raise 33 funds for GNU development.--> 34<meta http-equiv="Content-Style-Type" content="text/css"> 35<style type="text/css"><!-- 36 pre.display { font-family:inherit } 37 pre.format { font-family:inherit } 38 pre.smalldisplay { font-family:inherit; font-size:smaller } 39 pre.smallformat { font-family:inherit; font-size:smaller } 40 pre.smallexample { font-size:smaller } 41 pre.smalllisp { font-size:smaller } 42 span.sc { font-variant:small-caps } 43 span.roman { font-family:serif; font-weight:normal; } 44 span.sansserif { font-family:sans-serif; font-weight:normal; } 45--></style> 46<link rel="stylesheet" type="text/css" href="../cs.css"> 47</head> 48<body> 49<div class="node"> 50<a name="Zero-Length"></a> 51<p> 52Next: <a rel="next" accesskey="n" href="Variable-Length.html#Variable-Length">Variable Length</a>, 53Previous: <a rel="previous" accesskey="p" href="Named-Address-Spaces.html#Named-Address-Spaces">Named Address Spaces</a>, 54Up: <a rel="up" accesskey="u" href="C-Extensions.html#C-Extensions">C Extensions</a> 55<hr> 56</div> 57 58<h3 class="section">6.17 Arrays of Length Zero</h3> 59 60<p><a name="index-arrays-of-length-zero-2344"></a><a name="index-zero_002dlength-arrays-2345"></a><a name="index-length_002dzero-arrays-2346"></a><a name="index-flexible-array-members-2347"></a> 61Zero-length arrays are allowed in GNU C. They are very useful as the 62last element of a structure which is really a header for a variable-length 63object: 64 65<pre class="smallexample"> struct line { 66 int length; 67 char contents[0]; 68 }; 69 70 struct line *thisline = (struct line *) 71 malloc (sizeof (struct line) + this_length); 72 thisline->length = this_length; 73</pre> 74 <p>In ISO C90, you would have to give <code>contents</code> a length of 1, which 75means either you waste space or complicate the argument to <code>malloc</code>. 76 77 <p>In ISO C99, you would use a <dfn>flexible array member</dfn>, which is 78slightly different in syntax and semantics: 79 80 <ul> 81<li>Flexible array members are written as <code>contents[]</code> without 82the <code>0</code>. 83 84 <li>Flexible array members have incomplete type, and so the <code>sizeof</code> 85operator may not be applied. As a quirk of the original implementation 86of zero-length arrays, <code>sizeof</code> evaluates to zero. 87 88 <li>Flexible array members may only appear as the last member of a 89<code>struct</code> that is otherwise non-empty. 90 91 <li>A structure containing a flexible array member, or a union containing 92such a structure (possibly recursively), may not be a member of a 93structure or an element of an array. (However, these uses are 94permitted by GCC as extensions.) 95</ul> 96 97 <p>GCC versions before 3.0 allowed zero-length arrays to be statically 98initialized, as if they were flexible arrays. In addition to those 99cases that were useful, it also allowed initializations in situations 100that would corrupt later data. Non-empty initialization of zero-length 101arrays is now treated like any case where there are more initializer 102elements than the array holds, in that a suitable warning about "excess 103elements in array" is given, and the excess elements (all of them, in 104this case) are ignored. 105 106 <p>Instead GCC allows static initialization of flexible array members. 107This is equivalent to defining a new structure containing the original 108structure followed by an array of sufficient size to contain the data. 109I.e. in the following, <code>f1</code> is constructed as if it were declared 110like <code>f2</code>. 111 112<pre class="smallexample"> struct f1 { 113 int x; int y[]; 114 } f1 = { 1, { 2, 3, 4 } }; 115 116 struct f2 { 117 struct f1 f1; int data[3]; 118 } f2 = { { 1 }, { 2, 3, 4 } }; 119</pre> 120 <p class="noindent">The convenience of this extension is that <code>f1</code> has the desired 121type, eliminating the need to consistently refer to <code>f2.f1</code>. 122 123 <p>This has symmetry with normal static arrays, in that an array of 124unknown size is also written with <code>[]</code>. 125 126 <p>Of course, this extension only makes sense if the extra data comes at 127the end of a top-level object, as otherwise we would be overwriting 128data at subsequent offsets. To avoid undue complication and confusion 129with initialization of deeply nested arrays, we simply disallow any 130non-empty initialization except when the structure is the top-level 131object. For example: 132 133<pre class="smallexample"> struct foo { int x; int y[]; }; 134 struct bar { struct foo z; }; 135 136 struct foo a = { 1, { 2, 3, 4 } }; // <span class="roman">Valid.</span> 137 struct bar b = { { 1, { 2, 3, 4 } } }; // <span class="roman">Invalid.</span> 138 struct bar c = { { 1, { } } }; // <span class="roman">Valid.</span> 139 struct foo d[1] = { { 1 { 2, 3, 4 } } }; // <span class="roman">Invalid.</span> 140</pre> 141 </body></html> 142 143