comp.lang.ada
 help / color / mirror / Atom feed
From: Hyman Rosen <hyrosen@mail.com>
Subject: Re: Zero length Objects
Date: Thu, 01 Jul 2004 11:57:39 -0400
Date: 2004-07-01T11:57:39-04:00	[thread overview]
Message-ID: <1088697459.558708@master.nyc.kbcfp.com> (raw)
In-Reply-To: <cc193q$mt96@cui1.lmms.lmco.com>

Xenos wrote:
> But C++ does compensate for this somewhat.  Empty base classes (used for
> traits and the like) will not take any space in derived classes.  For
> example:
> 
> class Empty {};
> class Derived : public Empty { char a; };
> Derived D[1000000];
> 
> Here, Empty is not increase the size of D at all.

But it's more complicated than that, because C++ does have the
similar rule that no distinct objects of the same type can have
the same address. Expanding on your example,

     struct Empty1 { }; struct Empty2 { };
     struct Derived : Empty1, Empty2 { char a; };
     Derived D[1000000];

The optimization still applies, and sizeof(Derived)
can still be one. But do as follows:

     struct Empty { };
     struct E1 : Empty { }; struct E2 : Empty { };
     struct Derived : E1, E2 { char a; };
     Derived D[1000000];

Now, Derived has two distinct subobjects both of type
Empty. They cannot be allocated at the same address, so
it's likely that sizeof(Derived) is > 1. Although, in
this case the compiler can get really clever and place
one of the Empty objects before the char and one after.
But cases can be concocted where extra pad bytes are
definitely required to avoid messing up object identity.




  reply	other threads:[~2004-07-01 15:57 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-06-30  5:55 Zero length Objects Robert C. Leif
2004-06-30 20:32 ` Jacob Sparre Andersen
2004-06-30 21:01 ` Frank J. Lhota
2004-07-01  0:02   ` Nick Roberts
2004-07-01  1:28     ` Georg Bauhaus
2004-07-01 10:37       ` Björn Persson
2004-07-01 11:25     ` Larry Kilgallen
2004-07-01 14:11       ` Nick Roberts
2004-07-01 15:42         ` Larry Kilgallen
2004-07-01 14:06     ` Xenos
2004-07-01 15:26       ` Nick Roberts
2004-07-02  1:06         ` Jeffrey Carter
2004-07-01  0:47   ` Brian May
2004-07-01 13:32     ` Frank J. Lhota
2004-07-01 14:52       ` Nick Roberts
2004-07-01 15:03         ` Xenos
2004-07-01 15:57           ` Hyman Rosen [this message]
2004-07-01 16:05             ` Xenos
2004-07-02 15:02               ` Frank J. Lhota
2004-07-02 15:11                 ` Adrian Knoth
2004-07-02 15:43                   ` Frank J. Lhota
2004-07-02 19:01                     ` Vinzent 'Gadget' Hoefler
2004-07-02 19:07                       ` Adrian Knoth
2004-07-02 19:25                         ` Vinzent 'Gadget' Hoefler
2004-07-02 21:06                           ` Xenos
2004-07-02 21:56                             ` Vinzent 'Gadget' Hoefler
  -- strict thread matches above, loose matches on Subject: below --
2004-07-02  8:30 Christoph Karl Walter Grein
2004-07-06 11:59 ` Nick Roberts
2004-07-06 22:14   ` Randy Brukardt
2004-07-06 22:28     ` Nick Roberts
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox