Consider the following struct
struct a{ uint16_t foo1 uint16_t foo2 uint32_t foo3 uint64_t foo4 uint16_t foo5 uint16_t foo6 }__attribute__(packed); It's 20 bytes long. That's fine, as everything within the struct is aligned to word boundaries.
However, what happens if a well-meaning developer does the following:
static struct a foo; static uint64_t b; This would, theoretically, misalign b across a word boundary.
Interestingly, gcc seems to align foo to 16 bytes, however (if my knowledge of assembly is correct), it allows b to be misaligned:
.local foo.2476 .comm foo.2476,20,16 .local b.2477 .comm b.2477,8,8 Am I missing something here, or is this an example of the dangers of struct packing?
struct a foo[2]would have misalignment. So what aboutstruct a foo1; struct a foo2;, are you suggesting the compiler will add padding anyways?bis misaligned if it starts at a 4-byte boundary? Alignment requirements are architecture-specific, and the alignment requirement for a multibyte type can be smaller than that type's size.