I have found an "unexpected" behavior of the following code, which modifies a static const int by derefering its pointer:
#include <stdio.h>
static const int a;
int main(int argc, char *argv[])
{
printf("a=%d\n", a);
*((int *) &a) = 120;
printf("a=%d\n", *((volatile *) &a));
printf("a=%d\n", a);
return 0;
}
The output of the program compiled with GCC Debian 4.9.2-10 (as well as Debian 4.4.5-8 with -Os) is:
a=0
a=120
a=0
It seems that the compiler optimises the variable value, unless a volatile cast is used. No warning is given during the compilation, and I find strange that the program behaves different in Debian 4.4.5-8 with -Os. Indeed, if no size optimisation is requested, it prints the "expected" value of 120 on the third line.
Is this the normal behavior?
Thanks a lot in advance for your comments