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: RFC: attribute "unpadded"


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.

For this reason, Alexandre Oliva's suggestion to use the attribute in
the definition of `struct B' to control the layout of `struct B' make
more sense to me.  Not because it's type-theoretically right, but simply
because that gives the same results as C++.

-- Jamie


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