Understanding the struct alignment and padding calculator — concept and principle
A C or C++ compiler usually inserts unused padding bytes inside a struct so that each field starts at an address that suits its type. This calculator lays out your fields in the order given, shows each field's offset, the total struct size and the padding, and compares the size you would get by ordering fields from largest to smallest. It helps when designing binary formats and reducing memory use in large arrays.
How it is calculated
The calculator assumes natural alignment, meaning each field's alignment equals its size.
- Start at offset 0. For each field of size s, round the current offset up to a multiple of s: offset = ⌈offset ÷ s⌉ × s.
- Place the field there and advance the offset by s.
- After the last field, round the end up to a multiple of the largest field size; that is the total size.
- Padding bytes = total size − sum of field sizes.
- Sorted size repeats the same layout with the fields ordered from largest to smallest.
The principle
Processors load data most efficiently when an n-byte value sits at an address divisible by n; on some architectures a misaligned access is slower and on others it faults. Compilers therefore follow the platform ABI and pad fields. The trailing padding exists because structs are placed back-to-back in arrays: the size must be a multiple of the struct's alignment so that every element's fields stay aligned. Sorting fields from largest to smallest lets the smaller fields fill gaps that would otherwise be wasted.
Worked example
- Fields in order: 1, 4, 2, 8, 1 bytes (16 bytes of data).
- 1-byte field at 0; next free offset 1.
- 4-byte field: round 1 up to 4, so it sits at 4; next 8.
- 2-byte field at 8; next 10. 8-byte field: round 10 up to 16; next 24.
- 1-byte field at 24; end 25. Round 25 up to a multiple of 8: total size 32.
- Padding: 32 − 16 = 16 bytes. Sorted (8, 4, 2, 1, 1) the offsets are 0, 8, 12, 14, 15, ending at 16, so the sorted size is 16.
How to read the result
Here half of the struct is padding, and simply reordering the fields halves its size. For an array of a million such structs that saves 16 MB. Zero padding means the layout is already tight.
Common mistakes
- Assuming the struct size equals the sum of its fields.
- Forgetting the trailing padding at the end of the struct.
- Treating an array field like char[16] as needing 16-byte alignment; arrays align to their element type, which this simple model does not handle.
Frequently asked questions
Does the C standard require padding?
It allows implementation-defined padding after any member, but not before the first member, whose offset is always 0.
Does reordering change program behaviour?
Not logically, but it changes the binary layout, so it breaks compatibility with files and protocols that expect the old order.
Sources: ISO/IEC 9899 (C standard), 6.7.2.1 Structure and union specifiers · System V Application Binary Interface, AMD64 Architecture Processor Supplement