This is the mail archive of the gcc-help@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: -Wpacked in gcc manual page question


On 10/30/2013 11:04 AM, Norbert van Bolhuis wrote:
> I don't understand the -Wpacked description from the GCC manual page, it reads:
> 
>        -Wpacked
>             Warn if a structure is given the packed attribute, but the packed attribute has no effect on the layout or size of the structure.  Such structures may be 
> mis-aligned for little benefit.  For instance, in this
>             code, the variable "f.x" in "struct bar" will be misaligned even though "struct bar" does not itself have the packed attribute:
> 
>                     struct foo {
>                       int x;
>                       char a, b, c, d;
>                     } __attribute__((packed));
>                     struct bar {
>                       char z;
>                       struct foo f;
>                     };
> 
> 
> why does the "__attribute__((packed))" for struct foo cause misalignment for struct bar ?

__attribute__((packed)) is applied piecewise to each member of a packed
struct.

> I would expect misalignment only if struct bar is defined with __attribute__((packed))
> or its member f.
> 
> I thought __attribute__((packed)) only works for (the members of) the struct, but apparently
> it tries to avoid 3 bytes extra padding in struct bar. However, sizeof(struct bar) = 12 !?
> so there still is 3 bytes padding *and* misalignment. Why would anyone ever want this gcc behaviour ?

Try an array of struct bar.

Andrew.


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