Search code examples
cmemorybinarybit-shift

Naturally aligned memory address


I need to extract a memory address from within an existing 64-bit value, and this address points to a 4K array, the starting value is:

0x000000030c486000

The address I need is stored within bits 51:12, so I extract those bits using:

address = start >> 12 & 0x0000007FFFFFFFFF

This leaves me with the address of:

0x000000000030c486

However, the documentation I'm reading states that the array stored at the address is 4KB in size, and naturally aligned.

I'm a little bit confused over what naturally aligned actually means. I know with page aligned stuff the address normally ends with '000' (although I could be wrong on that).

I'm assuming that as the address taken from the starting value is only 40 bits long, I need to perform an additional bitshifting operation to arrange the bits so that they can be correctly interpreted any further.

If anyone could offer some advice on doing this, I'd appreciate it.

Thanks


Solution

  • Normally, "naturally aligned" means that any item is aligned to at least a multiple of its own size. For example, a 4-byte object is aligned to an address that's a multiple of 4, an 8-byte object is aligned to an address that's a multiple of 8, etc.

    For an array, you don't normally look at the size of the whole array, but at the size of an element of the array.

    Likewise, for a struct or union, you normally look at the size of the largest element.