Zero-Length.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): Zero Length</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Zero Length">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Zero Length">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="C-Extensions.html#C-Extensions" rel="up" title="C Extensions">
  30. <link href="Empty-Structures.html#Empty-Structures" rel="next" title="Empty Structures">
  31. <link href="Named-Address-Spaces.html#Named-Address-Spaces" rel="prev" title="Named Address Spaces">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Zero-Length"></a>
  63. <div class="header">
  64. <p>
  65. 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> &nbsp; [<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>
  66. </div>
  67. <hr>
  68. <a name="Arrays-of-Length-Zero"></a>
  69. <h3 class="section">6.17 Arrays of Length Zero</h3>
  70. <a name="index-arrays-of-length-zero"></a>
  71. <a name="index-zero_002dlength-arrays"></a>
  72. <a name="index-length_002dzero-arrays"></a>
  73. <a name="index-flexible-array-members"></a>
  74. <p>Zero-length arrays are allowed in GNU C. They are very useful as the
  75. last element of a structure that is really a header for a variable-length
  76. object:
  77. </p>
  78. <div class="smallexample">
  79. <pre class="smallexample">struct line {
  80. int length;
  81. char contents[0];
  82. };
  83. struct line *thisline = (struct line *)
  84. malloc (sizeof (struct line) + this_length);
  85. thisline-&gt;length = this_length;
  86. </pre></div>
  87. <p>In ISO C90, you would have to give <code>contents</code> a length of 1, which
  88. means either you waste space or complicate the argument to <code>malloc</code>.
  89. </p>
  90. <p>In ISO C99, you would use a <em>flexible array member</em>, which is
  91. slightly different in syntax and semantics:
  92. </p>
  93. <ul>
  94. <li> Flexible array members are written as <code>contents[]</code> without
  95. the <code>0</code>.
  96. </li><li> Flexible array members have incomplete type, and so the <code>sizeof</code>
  97. operator may not be applied. As a quirk of the original implementation
  98. of zero-length arrays, <code>sizeof</code> evaluates to zero.
  99. </li><li> Flexible array members may only appear as the last member of a
  100. <code>struct</code> that is otherwise non-empty.
  101. </li><li> A structure containing a flexible array member, or a union containing
  102. such a structure (possibly recursively), may not be a member of a
  103. structure or an element of an array. (However, these uses are
  104. permitted by GCC as extensions.)
  105. </li></ul>
  106. <p>Non-empty initialization of zero-length
  107. arrays is treated like any case where there are more initializer
  108. elements than the array holds, in that a suitable warning about &ldquo;excess
  109. elements in array&rdquo; is given, and the excess elements (all of them, in
  110. this case) are ignored.
  111. </p>
  112. <p>GCC allows static initialization of flexible array members.
  113. This is equivalent to defining a new structure containing the original
  114. structure followed by an array of sufficient size to contain the data.
  115. E.g. in the following, <code>f1</code> is constructed as if it were declared
  116. like <code>f2</code>.
  117. </p>
  118. <div class="smallexample">
  119. <pre class="smallexample">struct f1 {
  120. int x; int y[];
  121. } f1 = { 1, { 2, 3, 4 } };
  122. struct f2 {
  123. struct f1 f1; int data[3];
  124. } f2 = { { 1 }, { 2, 3, 4 } };
  125. </pre></div>
  126. <p>The convenience of this extension is that <code>f1</code> has the desired
  127. type, eliminating the need to consistently refer to <code>f2.f1</code>.
  128. </p>
  129. <p>This has symmetry with normal static arrays, in that an array of
  130. unknown size is also written with <code>[]</code>.
  131. </p>
  132. <p>Of course, this extension only makes sense if the extra data comes at
  133. the end of a top-level object, as otherwise we would be overwriting
  134. data at subsequent offsets. To avoid undue complication and confusion
  135. with initialization of deeply nested arrays, we simply disallow any
  136. non-empty initialization except when the structure is the top-level
  137. object. For example:
  138. </p>
  139. <div class="smallexample">
  140. <pre class="smallexample">struct foo { int x; int y[]; };
  141. struct bar { struct foo z; };
  142. struct foo a = { 1, { 2, 3, 4 } }; // <span class="roman">Valid.</span>
  143. struct bar b = { { 1, { 2, 3, 4 } } }; // <span class="roman">Invalid.</span>
  144. struct bar c = { { 1, { } } }; // <span class="roman">Valid.</span>
  145. struct foo d[1] = { { 1, { 2, 3, 4 } } }; // <span class="roman">Invalid.</span>
  146. </pre></div>
  147. <hr>
  148. <div class="header">
  149. <p>
  150. 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> &nbsp; [<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>
  151. </div>
  152. </body>
  153. </html>