data alignment
Data alignment is a rule about where in memory a multi-byte value is allowed to start. Memory is a long row of byte-sized cells, each with an address, and most processors prefer (or insist) that a value sit at an address that is a multiple of its size: a 4-byte integer at an address divisible by 4, an 8-byte value at one divisible by 8. Picture parking spaces painted for specific vehicle sizes — a bus must start in a bus-marked bay, not straddle two car spaces — that lining-up is alignment.
The reason is how the hardware actually fetches memory. A processor reads memory in fixed-size chunks (a word or a cache line), so an aligned 4-byte integer falls neatly inside one chunk and is grabbed in a single access. A misaligned value that straddles two chunks may require two reads plus extra work to stitch the pieces together — slower, and on some processors not allowed at all (an unaligned access causes a hardware fault that crashes the program). To keep things aligned, compilers quietly insert unused gap bytes, called padding, between the fields of a structure. This is why a struct's total size can be larger than the sum of its fields, and why reordering fields from largest to smallest can shrink it.
Alignment matters because it is a hidden factor in both performance and correctness. Most of the time the compiler handles it for you and you never notice. But it surfaces in real situations: laying out data structures to minimise wasted padding, casting a raw byte buffer to a typed pointer (which can produce a misaligned access and crash on strict hardware), and arranging arrays so that hot fields share a cache line. The honest point is that alignment is not optional decoration — it reflects the physical reality of how memory is fetched, and ignoring it can cost speed or, on unforgiving processors, cause a crash.
A struct holding a 1-byte char then a 4-byte int is not 5 bytes: the compiler inserts 3 padding bytes after the char so the int starts at an address divisible by 4, making the struct 8 bytes. Reordering the int before the char can remove the gap.
Padding bytes keep each field aligned, so a struct can be larger than the sum of its fields.
On strict processors an unaligned access is not merely slow — it faults and crashes. Casting a raw byte buffer to a typed pointer is a common way to trigger this, so alignment is a correctness issue, not just performance.