IT lexicon › Programming › Struct padding

Struct padding

Programming På svenska → Updated: 2026-11-06

The invisible bytes a compiler inserts into a struct so every field lands on an address its type requires.

The ABI says an int lives at an address divisible by four and a double at one divisible by eight. So struct { char a; int b; char c; } takes twelve bytes, not six: three bytes of nothing after a and three after c, so the next element of an array is aligned too. Move c ahead of b and the same data fits in eight.

C and C++ may never reorder fields on their own, because the layout is part of the interface with other code. Rust does it by default and demands #[repr(C)] for the same guarantee. #pragma pack removes the padding at the price of slow or outright illegal unaligned loads. The pahole tool shows where the holes are, and in the Linux kernel it is anything but obscure.

← Back to the lexicon