packed struct for Exact Layout
Lay out bits with no padding.
Layout Matters
Sometimes you need bytes laid out in an exact shape, like a hardware register or a network packet. That is when a packed struct shines.
Normal Structs Add Padding
A plain struct lets Zig reorder and pad fields for speed, so its in-memory size may be larger than the sum of its parts.
const Plain = struct {
flag: bool,
value: u32,
};All lessons in this course
- packed struct for Exact Layout
- Arbitrary-Width Integers like u3
- Bit Fields and Flags
- extern struct and ABI Layout