sizeof wrong for struct with final zero-length array

Greg McGary greg@mcgary.org
Sun May 28 00:03:00 GMT 2000


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


More information about the Gcc-bugs mailing list