C Reference ยท Types and Literals
Struct padding and alignment in C
Concept page on struct padding in C: alignment rules, why sizeof is bigger than the sum of members, reordering to save space, offsetof, alignof and packed structs.
The compiler is allowed to insert unused bytes, called padding, between struct members and after the last one. It does this so every member starts at an address that suits its type: on most machines a 4-byte int is expected at an address divisible by 4, and an 8-byte double at one divisible by 8. Misaligned access is slow on some CPUs and a crash on others.
As a result, sizeof(struct) is often larger than the sum of the member sizes, and the order of the members changes the total size. The exact numbers depend on the platform; the examples below show what the runner (x86-64, GCC) prints.
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
How the layout is decided
Each type has an alignment requirement, a power of two that you can read with alignof (C23 keyword; _Alignof earlier, with <stdalign.h> for the alignof spelling). The compiler lays members out in declaration order. For each member it advances the offset to the next multiple of that member's alignment, places it, then moves on. Finally the struct size is rounded up to a multiple of the struct's own alignment, which is the largest alignment of any member. That final rounding is tail padding, and it is what makes arrays of structs work: every element starts correctly aligned.
Reducing and controlling padding
- Order members from largest to smallest alignment. The
badandgoodstructs hold the same data, butgoodis smaller on the runner. - Group small members (
char,bool,short) together. alignas(16)(C23 keyword,_Alignasbefore) raises the alignment of a variable or member, for SIMD or cache-line separation.- Packed structs (
__attribute__((packed))or#pragma pack(1)) remove padding but are compiler extensions. Accessing a misaligned member can be slow or fault on some CPUs, and taking its address is hazardous. offsetof(type, member)from<stddef.h>gives the byte offset of a member. Bit-fields have no offset.
Usage notes
sizeofis the real size, including padding. Always use it formallocandmemcpy.- Do not write structs to files or sockets with
fwriteif the data must be portable: padding, endianness and sizes vary. Serialize member by member. - Padding bytes hold unspecified values, so
memcmpand hashing a struct's raw bytes are unreliable. Zero the struct first withmemsetif you must. - The layout is implementation-defined, but the order of members is guaranteed to follow declaration order, and the first member is at offset 0.
- Unions are as large as their largest member, rounded up to their alignment.
- Do not guess: print
sizeofandoffsetofon the target platform, or check them withstatic_assert.
Related
struct,alignofandalignas,sizeof, Bit-fields,stddef.h- Lessons: Padding and alignment, Structures