This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
sizeof wrong for struct with final zero-length array
- To: gcc-bugs at gcc dot gnu dot org
- Subject: sizeof wrong for struct with final zero-length array
- From: Greg McGary <greg at mcgary dot org>
- Date: Sun, 28 May 2000 00:02:34 -0700
- Cc: greg at mcgary dot org
The following testcases fail:
------------------------------------------------------------------------------
struct s { char a[0]; };
struct s s = { { 1, 2, 3, 4 } };
main ()
{
if (sizeof s == 0)
abort ();
exit (0);
}
------------------------------------------------------------------------------
struct s { char a[0]; };
main ()
{
struct s s = { { 1, 2, 3, 4 } };
if (sizeof s == 0)
abort ();
exit (0);
}
------------------------------------------------------------------------------
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. This extension is used for numerous instances of
`struct locale_data' in glibc. 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.
Assuming it's a bug, what's the proper fix? c_sizeof accepts a type
and returns its TYPE_SIZE_UNIT. Should it also accept decls and
return DECL_SIZE_UNIT in that case, or should an initialized decl of
such a struct have its TREE_TYPE set to a type variant that has the
true array-member size?
Greg