0PricingLogin
Zig Academy · Lesson

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

  1. packed struct for Exact Layout
  2. Arbitrary-Width Integers like u3
  3. Bit Fields and Flags
  4. extern struct and ABI Layout
← Back to Zig Academy