142
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.
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,
struct f1 {
int x; int y[];
} f1 = { 1, { 2, 3, 4 } };
struct f2 {
struct f1 f1; int data[3];
} f2 = { { 1 }, { 2, 3, 4 } };
The convenience of this extension is that
refer to
.
f2.f1
This has symmetry with normal static arrays, in that an array of unknown size is also written with
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:
struct foo { int x; int y[]; };
struct bar { struct foo z; };
struct foo a = { 1, { 2, 3, 4 } };
struct bar b = { { 1, { 2, 3, 4 } } };
struct bar c = { { 1, { } } };
struct foo d[1] = { { 1 { 2, 3, 4 } } };
6.13. Structures With No Members
GCC permits a C structure to have no members:
struct empty {
};
The structure will have size zero. In C++, empty structures are part of the language. G++ treats empty
structures as if they had a single member of type
is constructed as if it were declared like
f1
f1
Chapter 6. Extensions to the C Language Family
f2
has the desired type, eliminating the need to consistently
// Valid.
// Invalid.
// Valid.
// Invalid.
.
char
.
.
[]
Need help?
Do you have a question about the ENTERPRISE LINUX 4 and is the answer not in the manual?