📄 zero-length.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.6"><!--Copyright © 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. <p>Permission is granted to copy, distribute and/or modify this documentunder the terms of the GNU Free Documentation License, Version 1.2 orany later version published by the Free Software Foundation; with theInvariant Sections being "GNU General Public License" and "FundingFree Software", the Front-Cover texts being (a) (see below), and withthe Back-Cover Texts being (b) (see below). A copy of the license isincluded 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.--><meta http-equiv="Content-Style-Type" content="text/css"><style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller }--></style></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 thelast element of a structure which is really a header for a variable-lengthobject:<pre class="smallexample"> struct line { int length; char contents[0]; }; struct line *thisline = (struct line *) malloc (sizeof (struct line) + this_length); thisline->length = this_length; </pre> <p>In ISO C90, you would have to give <code>contents</code> a length of 1, whichmeans 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 isslightly different in syntax and semantics: <ul><li>Flexible array members are written as <code>contents[]</code> withoutthe <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 implementationof 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 containingsuch a structure (possibly recursively), may not be a member of astructure or an element of an array. (However, these uses arepermitted by GCC as extensions.) </ul> <p>GCC versions before 3.0 allowed zero-length arrays to be staticallyinitialized, as if they were flexible arrays. In addition to thosecases that were useful, it also allowed initializations in situationsthat would corrupt later data. Non-empty initialization of zero-lengtharrays is now treated like any case where there are more initializerelements than the array holds, in that a suitable warning about "excesselements in array" is given, and the excess elements (all of them, inthis case) are ignored. <p>Instead GCC allows static initialization of flexible array members. This is equivalent to defining a new structure containing the originalstructure 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 declaredlike <code>f2</code>.<pre class="smallexample"> 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 desiredtype, eliminating the need to consistently refer to <code>f2.f1</code>. <p>This has symmetry with normal static arrays, in that an array ofunknown size is also written with <code>[]</code>. <p>Of course, this extension only makes sense if the extra data comes atthe end of a top-level object, as otherwise we would be overwritingdata at subsequent offsets. To avoid undue complication and confusionwith initialization of deeply nested arrays, we simply disallow anynon-empty initialization except when the structure is the top-levelobject. For example:<pre class="smallexample"> 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 + -