In the following code:
#include <stdio.h>
int main(void)
{
int n = 74;
int * pn = &n;
short * sp;
//sp = (short *)&n; // <-- bad
sp = (short *)pn; // <-- fine
*sp = 4;
printf("%d\n", *sp);
return 0;
}
does the strict aliasing rule get broken? As far as I understand, it does, since a pointer to a short points to an int. But the compiler does not complain even if compiled with (assuming the code is in test.c)
gcc test.c -o test.exe -Wall -std=c99 -Wstrict-aliasing=2 -O3
However, it does complain if the line marked as bad is uncommented, and the one marked as fine is commented.
Can someone please explain does this case really break the rule? If so, why is it not detected by the compiler? If not, why not, since according to the standard it should?