user4182981
user4182981

Reputation:

Why are long long values aligned to an 8 byte boundary?

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?

Upvotes: 0

Views: 614

Answers (2)

mefathy
mefathy

Reputation: 781

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.

Upvotes: 3

Stephen C
Stephen C

Reputation: 718788

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.

Upvotes: 3

Related Questions