Good question. Consider this hypothetical type:
struct A { int n; bool flag; };
So an object of type A
should take five bytes (four for int plus one for bool), but actually it takes eight. Why?
The answer can be seen if you use this type:
const size_t N = 100; A a[N];
If each A
was only five bytes, then a[0]
will be aligned, but a[1]
, a[2]
and most other elements will not.
But why does alignment even matter? There are several reasons related to equipment. One reason is that recently / frequently used memory is cached in cache lines on a silicon processor for quick access. A aligned object smaller than the cache line always fits on one line (but see the interesting comments below), but an unaligned object can move two lines, losing cache.
In fact, there are even more fundamental hardware reasons associated with the fact that data transmitted by bytes are transmitted on a 32-bit or 64-bit data bus, not to mention cache lines. Not only does inconsistency clog the bus with additional samples (due to the fact that it was previously tied), but it will also force the registers to shift bytes as they arrive. Even worse, misalignment tends to confuse optimization logic (at least the Intel optimization guide says that this is true, although I don't have personal knowledge about this last point). Thus, misalignment is very bad in terms of performance.
As a rule, it is worth spending empty bytes for these reasons.
Update: The comments below are helpful. I recommend them.
source share