Alignment along 4-byte boundaries

Tony the Pony picture Tony the Pony · Aug 6, 2009 · Viewed 22.9k times · Source

I recently got thinking about alignment... It's something that we don't ordinarily have to consider, but I've realized that some processors require objects to be aligned along 4-byte boundaries. What exactly does this mean, and which specific systems have alignment requirements?

Suppose I have an arbitrary pointer:

unsigned char* ptr

Now, I'm trying to retrieve a double value from a memory location:

double d = **((double*)ptr);

Is this going to cause problems?

Answer

laalto picture laalto · Aug 6, 2009

It can definitely cause problems on some systems.

For example, on ARM-based systems you cannot address a 32-bit word that is not aligned to a 4-byte boundary. Doing so will result in an access violation exception. On x86 you can access such non-aligned data, though the performance suffers a little since two words have to fetched from memory instead of just one.