C++ bug in structure packing and inheritance
Sam Lantinga
slouken@devolution.com
Sat Dec 15 15:07:00 GMT 2001
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 a = 1 (should be 0)
size of b = 8 (should be 8)
size of c = 16 (should be 12)
offset of c.z = 12 (should be 8)
If this is correct, I would appreciate it if somebody could explain it.
Thanks,
-Sam Lantinga, Software Engineer, Blizzard Entertainment
More information about the Gcc
mailing list