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] | |
Mark Mitchell wrote:This comes up in C++ like so: struct A { virtual void f(); char c; }; struct B : public A { char c2 };Here in C++, `sizeof(struct A)' is 8, and `sizeof(struct B)' is also 8.Here, the size of B is 8, not 12, because "c2" is packed into the tail padding of A. If A were "unpadded", you could express this in C as: struct A __attribute__((unpadded)) { void *vptr; char c; }; struct B { struct A __base; char c2; };This does _not_ express the same thing in C, because this C version of `sizeof(struct A)' is 5.
You're correct -- but that's not how you would map the C++ ABI onto C.
In C, you would need two versions of each class type; the type when it
is used as a base class, and the type when it is a complete object.
So, you would do this:
struct A { void *vptr; char c; };
struct A_as_base __attribute__((unpadded)) { void *vptr; char c; };
struct B { struct A_as_base __base; char c2; };
Then everything matches up.
You need to have two versions of each class because the base version
needs to omit virtual base classes.
--
Mark Mitchell mark@codesourcery.com
CodeSourcery, LLC http://www.codesourcery.com
| Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
|---|---|---|
| Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |