⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zero-length.html

📁 gcc手册
💻 HTML
字号:
<html lang="en">

<head>

<title>Using the GNU Compiler Collection (GCC)</title>

<meta http-equiv="Content-Type" content="text/html">

<meta name="description" content="Using the GNU Compiler Collection (GCC)">

<meta name="generator" content="makeinfo 4.3">

<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">

<!--

Copyright &copy; 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,

1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.



   <p>Permission is granted to copy, distribute and/or modify this document

under the terms of the GNU Free Documentation License, Version 1.2 or

any later version published by the Free Software Foundation; with the

Invariant Sections being "GNU General Public License" and "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".



   <p>(a) The FSF's Front-Cover Text is:



   <p>A GNU Manual



   <p>(b) The FSF's Back-Cover Text is:



   <p>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.-->

</head>

<body>

<div class="node">

<p>

Node:<a name="Zero%20Length">Zero Length</a>,

Next:<a rel="next" accesskey="n" href="Variable-Length.html#Variable%20Length">Variable Length</a>,

Previous:<a rel="previous" accesskey="p" href="Hex-Floats.html#Hex%20Floats">Hex Floats</a>,

Up:<a rel="up" accesskey="u" href="C-Extensions.html#C%20Extensions">C Extensions</a>

<hr><br>

</div>



<h3 class="section">Arrays of Length Zero</h3>



   <p>Zero-length arrays are allowed in GNU C.  They are very useful as the

last element of a structure which is really a header for a variable-length

object:



<pre class="example">     struct line {

       int length;

       char contents[0];

     };

     

     struct line *thisline = (struct line *)

       malloc (sizeof (struct line) + this_length);

     thisline-&gt;length = this_length;

     </pre>



   <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>In ISO C99, you would use a <dfn>flexible array member</dfn>, which is

slightly different in syntax and semantics:



     <ul>

<li>Flexible array members are written as <code>contents[]</code> without

the <code>0</code>.



     <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>Flexible array members may only appear as the last member of a

<code>struct</code> that is otherwise non-empty.



     <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.) 

</ul>



   <p>GCC versions before 3.0 allowed zero-length arrays to be statically

initialized, as if they were flexible arrays.  In addition to those

cases that were useful, it also allowed initializations in situations

that would corrupt later data.  Non-empty initialization of zero-length

arrays is now 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>Instead 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. 

I.e. in the following, <code>f1</code> is constructed as if it were declared

like <code>f2</code>.



<pre class="example">     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>



<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>This has symmetry with normal static arrays, in that an array of

unknown size is also written with <code>[]</code>.



   <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:



<pre class="example">     struct foo { int x; int y[]; };

     struct bar { struct foo z; };

     

     struct foo a = { 1, { 2, 3, 4 } };        // Valid.

     struct bar b = { { 1, { 2, 3, 4 } } };    // Invalid.

     struct bar c = { { 1, { } } };            // Valid.

     struct foo d[1] = { { 1 { 2, 3, 4 } } };  // Invalid.

     </pre>



   </body></html>



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -