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


> There seems to be a bug in structure packing in that if you have
> two classes or structures that both inherit from a class with no
> data members, then they will be the wrong size.

> A simple example follows:

> #include <stddef.h>
> #include <stdio.h>

> struct a {
> };

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

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

> main()
> {
> 	printf("size of a = %d (should be 0)\n", sizeof(a));
> 	printf("size of b = %d (should be 8)\n", sizeof(b));
> 	printf("size of c = %d (should be 12)\n", sizeof(c));
> 	printf("offset of c.z = %d (should be 8)\n", offsetof(c,z));
> }

> Sample output with the current CVS of gcc is:
> 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.

Can anybody confirm or deny this?  GCC gurus, please speak up. :)

Thanks!
	-Sam Lantinga, Software Engineer, Blizzard Entertainment


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