This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: sizeof wrong for struct with final zero-length array


> The following testcases fail:

Thanks for your bug report. I believe the error is in the test cases,
not in gcc.

> According to the GCC manual, the variable-length array semantics for
> an initialized struct having a zero-length arrays as its final
> member is a GCC extension.

The entire "zero-length array" feature (*) of GCC is an
extension. However, C99 introduced a similar feature (you use a[]
instead of a[0] to indicate a "flexible array member"), so it seems
desirable that these two features get aligned, and that the GNU
extension is deprecated. So any changes to GCC should consider C99,
not the GNU extension.

The specific case of a structure containing *only* a flexible array
member is not supported in C99, however, I believe that your point
applies to all other cases as well, right?

> I'm conviced it's a bug that sizeof reports the size of the type
> rather than the size of the decl, but I'd like confirmation first
> before I checkin these test cases.

If you could follow the previous rationale, I suggest we draw the
semantics of that feature from the C standard:

6.7.2.1/15
# This is called a flexible array member, and the size of the
# structure shall be equal to the offset of the last element of an
# otherwise identical structure that replaces the flexible array
# member with an array of unspecified length.

6.5.3.4/2
# The sizeof operator yields the size (in bytes) of its operand, which
# may be an expression or the parenthesized name of a type. The size
# is determined from the type of the operand.

So in a sizeof expression, the size of the object never matters; it is
always the size of the type that counts, and for a structure with a
FAM, the size of the array does not matter (only the alignment of the
array type may matter).

Regards,
Martin

(*) Please don't use the term VLA or variable-length array to refer to
that feature - this is a different extension.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]