This is the mail archive of the
mailing list for the GCC project.
Re: C99 conformance bug in gcc-3.1
On 30-May-2002, Momchil Velikov <email@example.com> wrote:
> As far as I can read through §126.96.36.199/[#16] the standard actually
> _allows_ for more space efficient layout for structures with flexible
> array members by recognizing that this kind of structures are usually
> not put in arrays, thus there's no need to align the size of the
> structure to the maximum member alignment.
> Of course, there's a potential problem if one indeed puts the
> structure in array.
> Is the following equality required by the standard:
> struct foo a[DIM];
> (char *) &a[i] == (char *) &a[i-1] + sizeof (struct foo) ?
Reading more, I see that according to ISO C99, it's a constraint violation
to put such a structure in an array (see 188.8.131.52/[#2]). So no, this
equality is not guaranteed; instead, a diagnostic is required,
because `struct foo a[DIM]' violates a constraint.
Hence I guess that you are right: in C99, there's no need to align the
size of the structure to the maximum member alignment.
So I guess the desired output for C99 flexible array members would be this:
sizeof(struct foo) = 6 <== gcc currently gives 8
offsetof(struct foo, pad) = 6
offsetof(struct bar, pad) = 6
This is potentially more efficient than what gcc currently uses.
But this only works for C99 flexible array members, not for GNU C's
zero-length array members.
I guess my argument earlier about breaking binary backwards compatibility
is weak, since previous versions of gcc used different syntax for this
feature (`' rather than `').
Still it would be rather odd if structs defined using C99 flexible array
members had different sizes than equivalent arrays defined using GNU
C's zero-length arrays. However, that seems hard to avoid, given that
GNU C permits structs with zero-sized array members to be elements of
other arrays or structs, and thus requires them to be padded to ensure
appropriate alignment, whereas C99 forbids adding padding after the
flexible array member.
Fergus Henderson <firstname.lastname@example.org> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.