I know that a double
is aligned to an 8-byte boundary because this is more helpful for floating-point instructions. But why is a long long
also aligned to an 8 byte boundary?

- 40,716
- 16
- 83
- 128
-
What's the size of your `long double`? – Kerrek SB Jan 10 '15 at 01:56
2 Answers
The most likely explanation is that the hardware architecture gives faster long long
reads and writes when they are 8 byte aligned.
For the record, reasoning for making the double
type 8 byte aligned would be the same. The actual floating-point arithmetic operations are implemented using register to register instructions. Memory alignment isn't relevant for register to register operations. Memory alignment is only relevant to the performance of instructions that involve memory reads and writes.

- 698,415
- 94
- 811
- 1,216
-
Good to know that aligning `double` and `long long` to an 8 byte boundary is for the same reason. – Jan 09 '15 at 23:06
-
@SteveJessop - Indeed! And I wouldn't be surprised if some mainstream C / C++ compilers allowed you relax the alignment constraints via compiler options (... at the cost of slower code). – Stephen C Jan 09 '15 at 23:49
A possible explanation is that both long long
and double
variables are 8-bytes long. When you do not align them to an 8-byte boundary, they can potentially span two pages of memory if a variable starts 3 bytes before the end of a page and keeps 5 bytes in the following page. This can cause a performance overhead because reading that variable may require fetching the two pages of memory (and writing to it would modify two pages of memory instead of one). There are other low-level performance implications that are discussed here.
-
2That goes for cache line boundaries too, and they're much more likely to be crossed since there are far more of them. – harold Jan 10 '15 at 08:01