Consider the following C program:
int main(void)
{
char string[10] __attribute__ ((aligned(32)));
int i;
int *intp = (int*)(string + 1 );
printf("string: 0x%x, intp: 0x%x\n", string, intp);
for (i=0; i<10; i++)
{
string[i] = 10;
}
dump(string);
printf("*intp: 0x%x\n", *intp);
*intp = 0xEEEEEEEE;
dump(string);
return 0;
}
So I was basically forcing CPU to access a 32 bit data (int) at a misaligned address. TBH I was hoping for a segfault on my ARM9 board. But instead I got some interesting/confusing result:
After setting intp to 0xEEEEEEEE, dumping of string shows:
0xee, 0xee, 0xee, 0xee, 0xa, 0xa, 0xa, 0xa, 0xa, 0xa
So the code actually changed the first element in string! Why?
Thanks,
Most probably the CPU “rounds” misaligned addresses, so that when you pass a misaligned address to a certain instruction, the hardware finds the closest boundary and performs the designated operation on that address.
This might really answer your question: