#Do I need to align nested structs too if my goal is packing?

1 messages · Page 1 of 1 (latest)

tranquil solstice
#

I think this code would lead to issues across different architectures.

cunning stirrup
#

the alignment of a struct is the alignment of the field with strictest alignment - so buz's type's alignment is 1

valid sorrel
tranquil solstice
valid sorrel
#

no?

#

packed in C does not mean bit-packed

tranquil solstice
tranquil solstice
#

Do you just never read from such a variable?

valid sorrel
#

the storage for this struct would have to be align(4) in C yes, but the fields of the struct can be align(1)

#

most architectures don't actually care though, underaligned reads just tend to be slower

#

the align(1) on buz does nothing, because the type of the field it is storing is already align(1)

#

if you want to reduce or increase the alignment to store that type, yes

#

u8 is already align(1)

#

if you add it everywhere, it will just work
if you miss it somewhere that isn't align(1) already, theres a chance you'll get padding, and the alignment of the overall type will increase

strange mantle
#

you would only need it on the struct if you were trying to put a struct that wasn't already align(1) inside https://godbolt.org/z/nYPT9xhKv

So to get the behavior you want you should put align(1) on every field, but you needn't annotate structs that are already align(1)