Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

195 lines
8.5KB

  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-2020 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 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Zero Length (Using the GNU Compiler Collection (GCC))</title>
  21. <meta name="description" content="Zero Length (Using the GNU Compiler Collection (GCC))">
  22. <meta name="keywords" content="Zero Length (Using the GNU Compiler Collection (GCC))">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  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.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Zero-Length"></a>
  62. <div class="header">
  63. <p>
  64. 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>
  65. </div>
  66. <hr>
  67. <a name="Arrays-of-Length-Zero"></a>
  68. <h3 class="section">6.18 Arrays of Length Zero</h3>
  69. <a name="index-arrays-of-length-zero"></a>
  70. <a name="index-zero_002dlength-arrays"></a>
  71. <a name="index-length_002dzero-arrays"></a>
  72. <a name="index-flexible-array-members"></a>
  73. <p>Declaring zero-length arrays is allowed in GNU C as an extension.
  74. A zero-length array can be useful as the last element of a structure
  75. that is really a header for a variable-length object:
  76. </p>
  77. <div class="smallexample">
  78. <pre class="smallexample">struct line {
  79. int length;
  80. char contents[0];
  81. };
  82. struct line *thisline = (struct line *)
  83. malloc (sizeof (struct line) + this_length);
  84. thisline-&gt;length = this_length;
  85. </pre></div>
  86. <p>Although the size of a zero-length array is zero, an array member of
  87. this kind may increase the size of the enclosing type as a result of tail
  88. padding. The offset of a zero-length array member from the beginning
  89. of the enclosing structure is the same as the offset of an array with
  90. one or more elements of the same type. The alignment of a zero-length
  91. array is the same as the alignment of its elements.
  92. </p>
  93. <p>Declaring zero-length arrays in other contexts, including as interior
  94. members of structure objects or as non-member objects, is discouraged.
  95. Accessing elements of zero-length arrays declared in such contexts is
  96. undefined and may be diagnosed.
  97. </p>
  98. <p>In the absence of the zero-length array extension, in ISO C90
  99. the <code>contents</code> array in the example above would typically be declared
  100. to have a single element. Unlike a zero-length array which only contributes
  101. to the size of the enclosing structure for the purposes of alignment,
  102. a one-element array always occupies at least as much space as a single
  103. object of the type. Although using one-element arrays this way is
  104. discouraged, GCC handles accesses to trailing one-element array members
  105. analogously to zero-length arrays.
  106. </p>
  107. <p>The preferred mechanism to declare variable-length types like
  108. <code>struct line</code> above is the ISO C99 <em>flexible array member</em>,
  109. with slightly different syntax and semantics:
  110. </p>
  111. <ul>
  112. <li> Flexible array members are written as <code>contents[]</code> without
  113. the <code>0</code>.
  114. </li><li> Flexible array members have incomplete type, and so the <code>sizeof</code>
  115. operator may not be applied. As a quirk of the original implementation
  116. of zero-length arrays, <code>sizeof</code> evaluates to zero.
  117. </li><li> Flexible array members may only appear as the last member of a
  118. <code>struct</code> that is otherwise non-empty.
  119. </li><li> A structure containing a flexible array member, or a union containing
  120. such a structure (possibly recursively), may not be a member of a
  121. structure or an element of an array. (However, these uses are
  122. permitted by GCC as extensions.)
  123. </li></ul>
  124. <p>Non-empty initialization of zero-length
  125. arrays is treated like any case where there are more initializer
  126. elements than the array holds, in that a suitable warning about &ldquo;excess
  127. elements in array&rdquo; is given, and the excess elements (all of them, in
  128. this case) are ignored.
  129. </p>
  130. <p>GCC allows static initialization of flexible array members.
  131. This is equivalent to defining a new structure containing the original
  132. structure followed by an array of sufficient size to contain the data.
  133. E.g. in the following, <code>f1</code> is constructed as if it were declared
  134. like <code>f2</code>.
  135. </p>
  136. <div class="smallexample">
  137. <pre class="smallexample">struct f1 {
  138. int x; int y[];
  139. } f1 = { 1, { 2, 3, 4 } };
  140. struct f2 {
  141. struct f1 f1; int data[3];
  142. } f2 = { { 1 }, { 2, 3, 4 } };
  143. </pre></div>
  144. <p>The convenience of this extension is that <code>f1</code> has the desired
  145. type, eliminating the need to consistently refer to <code>f2.f1</code>.
  146. </p>
  147. <p>This has symmetry with normal static arrays, in that an array of
  148. unknown size is also written with <code>[]</code>.
  149. </p>
  150. <p>Of course, this extension only makes sense if the extra data comes at
  151. the end of a top-level object, as otherwise we would be overwriting
  152. data at subsequent offsets. To avoid undue complication and confusion
  153. with initialization of deeply nested arrays, we simply disallow any
  154. non-empty initialization except when the structure is the top-level
  155. object. For example:
  156. </p>
  157. <div class="smallexample">
  158. <pre class="smallexample">struct foo { int x; int y[]; };
  159. struct bar { struct foo z; };
  160. struct foo a = { 1, { 2, 3, 4 } }; // <span class="roman">Valid.</span>
  161. struct bar b = { { 1, { 2, 3, 4 } } }; // <span class="roman">Invalid.</span>
  162. struct bar c = { { 1, { } } }; // <span class="roman">Valid.</span>
  163. struct foo d[1] = { { 1, { 2, 3, 4 } } }; // <span class="roman">Invalid.</span>
  164. </pre></div>
  165. <hr>
  166. <div class="header">
  167. <p>
  168. 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>
  169. </div>
  170. </body>
  171. </html>