#What's the best way to have an unpadded struct with a fixed mem layout?

1 messages · Page 1 of 1 (latest)

vale moat
#

extern struct follows the layout of structs for the C ABI

vale moat
#

an array of f32s will behave the same everywhere

#

and there won't be any padding, because its all aligned the same

gleaming silo
#

extern struct defines a fixed memory layout (fields in declaration order, with padding to align each value to its type's alignment) which matches how C structs are laid out in practice. Technically a compliant C compiler can pad however much it wants, but in practice literally every ABI specifies behaviour that fits into this pattern, possibly just with different alignments for different types

#

If you want fields to be unaligned byte-packed, you can specify align(1) on your fields to avoid padding

gleaming silo
#

In your case, you don't need any align specifiers, as everything's a f32 so you won't have padding

#

So all you need is just extern structs