Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alignment along 4-byte boundaries

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?

like image 716
Tony the Pony Avatar asked Aug 06 '09 09:08

Tony the Pony


1 Answers

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 be fetched from memory instead of just one.

like image 91
laalto Avatar answered Sep 22 '22 17:09

laalto