When you use optimization with gcc, it can use certain assumptions based on the type of expressions to avoid repeating unnecessary reads and allowing variables to be stored in memory.
Your code has undefined behavior because you pointed to a long long pointer (which gcc resolves as extenstion) to an int pointer, and then you manipulate the object with a pointer to the object, as if it were an int . The pointer-to- int usually cannot point to an object of type long long , so gcc assumes that the operation that writes to int (via the pointer) will not affect the object of type long long .
Therefore, he has the right to cache the value of n between the time when it was originally assigned and the time at which it was subsequently printed. No valid write operation could change its value.
The specific key and documentation for reading is -fstrict-aliasing .
Charles Bailey
source share