This is the mail archive of the gcc@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]
Other format: [Raw text]

Re: C++ bug in structure packing and inheritance


On Dec 16, 2001, Sam Lantinga <slouken@devolution.com> wrote:

>> struct a {
>> };

>> struct b : public a {
>> float x;
>> float y;
>> };

>> struct c : public a {
>> b data;
>> float z;
>> };

>> size of c = 16 (should be 12)

> Interestingly, if either b or c are not derived from a, or the order
> of b and z within c is swapped, c ends up being the correct size.

> I spent a few hours mucking about in class.c, and my uneducated guess
> is that the overlay of the base class a followed by the overlay of the
> base class a (in b) is what's causing the incorrect sizing of c.

It's actually correct.  The sub-object a of class c must have
a different address from the sub-object a of class b, the first data
member of class c.  Thus, data's offset in c must be non-zero.

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                  aoliva@{cygnus.com, redhat.com}
CS PhD student at IC-Unicamp        oliva@{lsd.ic.unicamp.br, gnu.org}
Free Software Evangelist    *Please* write to mailing lists, not to me


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