I compiled the following with gcc
Code:
#include <stdio.h>
int main()
{
const int a = 5;
const int *aptr = &a;
int *ptr = (int*)aptr;
printf("%p %p %p\n", ptr, &a, aptr);
*ptr = 3;
printf("%d %d %d\n", *ptr, a, *aptr);
return 0;
}
and found that, without any flags, it prints 3 3 3, but with -O1 through -O3 it prints 3 5 3.
This simple answer to the question is of course that writing to a const variable gives undefined results
You could get a better answer by using the -S switch to generate assembly output.. but I'm not good at reading that.