Skip to content
UPEEC

Alignment and Padding

CPUs prefer data at addresses that are multiples of its size, so compilers add padding inside structs.

Intermediate
Comments 0
struct { char a; int b; char c; }  /* likely 12 bytes, not 6 */

Order members large-to-small to reduce padding. _Alignof, alignas (C11/C23) control alignment; packed structs can be slow or fault on some CPUs.

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.