The GNU documentation states that malloc is aligned to 16 byte multiples on 64 bit systems. Why is this?
If my understanding is correct, registers and all instructions operate on values that are a maximum of 8 bytes wide. Thus, it would seem that 8 byte alignment would be the requirement.
Notes:
- I've found some justification for why the stack pointer has to be 16-byte aligned but not other memory addresses
- I've seen some commenters say it is simply to reduce internal fragmentation which doesn't make a ton of sense since the documentation states they moved to 16-byte alignment specifically for
x86_64
CodePudding user response:
x86_64 uses xmm registers (heavily -- all fp stuff is done in xmm registers as the 8087 fp registers are deprecated), and xmm registers require 16-byte alignment for (efficient) access.
So most things in x86_64 (both the stack and heap allocated by malloc) are organized to always be 16-byte aligned, so the compiler is always free to use the 'aligned' instructions when xmm registers are involved and does not need to use the (possibly slower) unaligned instructions.
