#Do I need to align nested structs too if my goal is packing?
1 messages · Page 1 of 1 (latest)
the alignment of a struct is the alignment of the field with strictest alignment - so buz's type's alignment is 1
no it won't, it's equivalent to packed in C
It produces bit shifts?
Sorry, I don't understand.
But how do you perform a misaligned read? That's what I don't get.
Do you just never read from such a variable?
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
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)