C Struct Padding Calculator — offsets, padding and size

Paste a C struct to see member offsets, inserted padding and total size for 32-bit and 64-bit targets — and how to reorder it smaller.

Example: This struct is 32 bytes with 16 of padding, or 16 bytes if the members are reordered.

Check it against real silicon

Chiprun runs your firmware on an emulated microcontroller and hands back the UART output, so you can confirm these numbers rather than trusting them.

Chiprun docs

Formula

each member starts at a multiple of its alignment; the struct is padded to its strictest

Worked example

This struct is 32 bytes with 16 of padding, or 16 bytes if the members are reordered.

  1. each member starts at a multiple of its own alignment

    a @ 0, b @ 4, c @ 8, d @ 16, e @ 24

    32 bytes total

  2. struct size rounds up to its strictest alignment

    round 26 up to a multiple of 8

    32 bytes

Frequently asked questions

Why is my struct bigger than the sum of its members?

Padding. Each member must start at an offset that is a multiple of its own alignment, so the compiler inserts gaps. The struct is then padded at the end too, so that an array of them keeps every element aligned.

How do I make a struct smaller?

Reorder the members largest-alignment-first. It costs nothing at runtime and often removes most of the padding — this tool shows both layouts and the saving. Packing with a pragma also works but makes access unaligned, which is slower on x86 and faults on some ARM cores.

Why is there padding at the end of a struct?

So arrays work. If a struct contains an 8-byte member, every element in an array of it must start on an 8-byte boundary — which means the struct's size has to be a multiple of 8, even if the last member ends earlier.

Does this layout change between platforms?

Yes. `long` and pointers are 4 bytes on 32-bit and 8 on 64-bit, which shifts every subsequent offset. If a struct crosses a wire or a file format, use the fixed-width intN_t types and check the layout on both ends.

Related tools