C++ bug in structure packing and inheritance
Alexandre Oliva
aoliva@redhat.com
Sat Dec 15 23:57:00 GMT 2001
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
More information about the Gcc
mailing list