Reputation: 681
In libc's malloc(x), the function is said to return a pointer to a memory region of at least x bytes and the pointer is aligned to 8 bytes. What does this alignment mean? THank you.
Upvotes: 7
Views: 4533
Reputation: 86504
It means that the memory starts on an address that is a multiple of 8.
As for why you would even care: For memory that's not aligned, the CPU sometimes requires two accesses to read it all. In some cases, it won't even try and will just throw an error. The mention of "aligned to 8 bytes" is so that the caller knows whether it'll have to do any fudging of the pointer or not.
(Usually, you won't care -- the compiler takes care of most of the alignment issues for you. But the info's there in case you need it for some reason.)
Upvotes: 6
Reputation: 6089
Memory alignment describes the modulus of the addresses. So 8 byte alignment means that he addresses are multiples of eight.
This was critical on many older systems where addresses needed to be aligned on a 'word' boundary, often a multiple of four bytes or two bytes. If it wasn't properly aligned the program crashed with an 'alignment error'.
More recent machines fix this problem by loading from any address, but often that means that it takes few more cycles to load the data.
Upvotes: 1
Reputation: 97948
It means that the pointed address mod 8 is 0:
pointer % 8 == 0
This can be important for low level operations where it may impact correctness or efficiency. See also this answer.
Upvotes: 11