Richard Damon
9/5/2011 3:35:00 PM
On 9/5/11 2:49 AM, Sean Hamilton wrote:
> struct a { uint8_t b, c, d, e; };
> union b { uint8_t f[16]; struct a g[4]; };
>
> Is it guaranteed that b.f and b.g will have the same address and size?
> Is a struct with only one type of member like that equivalent to an
> array with regard to alignment and padding?
>
> Thanks in advance,
for union, the address of each member will be the same, that of the
union, no padding before the member is allowed.
The size may not be the same. Structs may have padding, and the compiler
may decide that it is helpful for all structs to have a certain minimal
alignment, which if it was larger than 4, might cause sizeof(struct a)
to be > 4. This is particularly likely on a word addresses machine,
where pointers to individual bytes requires a longer pointer to add byte
offsets, as a pointer to struct needs to have its format defined
independent of the detail of the contents of the struct.
So a Word Addressed machine with 64 bit words, would likely added 4
bytes of padding to struct a.