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]

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 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


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