Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working out address alignment

Tags:

c++

alignment

How do you work out the alignment of an address by just looking at it?

On a 32bit system, an address of 0x12345670 means it's 16 byte aligned (because of the 0 on the end) right?

So what about:

0x12345671 0x12345672 0x12345673

etc?

Cheers, Jon


1 Answers

An address A is n-aligned if A mod n is 0. So 0x12345671 and 0x12345673 are 1 aligned, and 0x12345672 is 2-aligned. 0x12345670 is, simultaneously, 1-aligned, 2-aligned, 4-aligned, 8-aligned, and 16-aligned. You only mention the highest power of two (as the lower powers are implied).

like image 199
Martin v. Löwis Avatar answered May 16 '26 18:05

Martin v. Löwis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!