#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char *page,*p; page=calloc(1,4); page+=4; p=page; if (page) { // if we change "page" here to "1", the result will be correct *((char **)page-1)=page; // save the address in its previous 4 bytes. (*((char ***)page-1))++; // add 4 to the saved address. } page=*((char **)page-1); //"page" should be assigned to the new value. printf("page=%08X p=%08X\n",page,p); // with the optimization level -O2/3, the value of page i s not changed. return 0; } Release: 3.2.2 Environment: sunOS 2.8 sparc How-To-Repeat: compile with and without O3/O2 options.
*** This bug has been marked as a duplicate of 9901 ***
State-Changed-From-To: closed->open State-Changed-Why: Closed as a duplicate by mistake.
State-Changed-From-To: open->closed State-Changed-Why: Not a bug. Your code breaks the ISO C aliasing rules. GCC automatically turns on -fstrict-aliasing at -O2 so you need to fix your code or compile it with -fno-strict-aliasing.