In a byte-addressable memory, a 4-byte word could in principle start at any byte. It is aligned when it starts at a multiple of its size:
- 2-byte words: even addresses (low bit
0) - 4-byte words: multiples of 4 (low two bits
00) - 8-byte words: multiples of 8 (low three bits
000)
Why bother? The memory hardware is built around whole words. An aligned word sits entirely inside one physical word and can be fetched in a single access. An unaligned word straddles two physical words: the hardware has to make two accesses and stitch the pieces together, or, on many processors, it simply refuses and raises an error.
Alignment also makes address arithmetic clean. For an aligned 4-byte word, the low two address bits are always 00, and the rest of the address is the word number.
Compilers lay out data aligned automatically, sometimes leaving unused padding bytes between fields to do it.