Let's assume a 32-bit CPU and 2-byte short
. I know 4-byte int
needs to be aligned at address multiple of 4 to avoid extra reads.
Questions:
- If a short is stored at
0x1
, the CPU can still read from0x0
in one operation. So, why do shorts need to be aligned at an address multiple of 2? - If a short is stored at
0x2
, why would it considered aligned and efficient since the CPU can only read from 0x0 and discard the first two bytes?
There is a question that is very similar to this, however, the answer only tells us the alignment requirement is the same for short
in the struct and the standalone variable. There is also a comment with 2 upvotes saying:
On many machines, accessing an N-byte quantity (at least for N in {1, 2, 4, 8, 16}) works most efficiently when the quantity is N-byte aligned. It's the way life is; get used to it because I doubt that chip manufacturers are going to change it just because you think it isn't the way it should be.
But why?