3 ms·
If your struct contains a field that is itself a struct, e.g. one containing three int32_t's, then that field has size 12, which is not a power of two.
by tomsmeding 1y ago
If your struct contains a field that is itself a struct, e.g. one containing three int32_t's, then that field has size 12, which is not a power of two.
- compiler-guy 1y agoDoesn't even need to be a struct within a struct: struct Foo { int8_t a; int8_t b; int8_t c; }; will have a size of 3 and an alignment of 1. Alignment is always power of 2, and size modulus alignment will always be zero. But alignment can be less than size, and size need not be a power of two, just have one power-of-two factor.
- bombela 1y agoBrain was not fully loaded yet, thank you. I don't think the struct in struct is relevant in your example by the way. i32 requires 4-bytes alignment. An array of a struct{3×i32} will be aligned to 12 bytes. Which maintains the 4 bytes alignment of the fields. If this structure is embedded into another one, it will be padded accordingly to the alignment requirement of the outer field. { { 3×i32 } i32 } will be 16 bytes. { { 3×i32 } i64 } will be 12, padding 4, 8. Totalling 24. I am assuming x86_64.