Document empty structures

Mark Mitchell mark@codesourcery.com
Mon Apr 14 17:01:00 GMT 2003


On Sun, 2003-04-13 at 20:43, Fergus Henderson wrote:
> On 11-Apr-2003, Geoffrey Keating <gkeating@apple.com> wrote:
> > Index: doc/extend.texi
> ...
> > +@node Empty Structures
> > +@section Structures With No Members
> > +@cindex empty structures
> > +@cindex zero-size structures
> > +
> > +GCC permits a C structure to have no members:
> > +
> > +@example
> > +struct empty @{
> > +@};
> > +@end example
> > +
> > +The structure will have size zero.  In C++, empty structures are part
> > +of the language, and the language standard says they have size 1.
> 
> I'm pretty sure that the C++ language standard does not say that empty
> structures have size 1 -- AFAIK it just says that they have non-zero size
> (C++98 section 9, paragraph 3).

Correct.  The C++ ABI says they have size 1.

Geoffrey, we also needs docs for arrays of these things.  (Are all the
elements at the same address or not?)

Frankly, I think instead of admitting we have this (C++-incompatible)
extension, we should just change those structures to have size one.

Some things go wrong when you have zero-sized objects; idioms like
"sizeof (a) / sizeof (a[0])" stop working, and you either break the
invariant that "a + sizeof(x)" is the same as "a[1]" or you have
multiple objects at the same address, or you have to tell people that
a[0] and a[1] are actually the same object.

We had a discussion about this some months back, including an attribute
for people who really wanted the old behavior.  I don't remember the
conclusions, if any...

-- 
Mark Mitchell
CodeSourcery, LLC
mark@codesourcery.com



More information about the Gcc-patches mailing list