Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The aliasing rules were designed to allow compilers more aggressive optimization. Basically, a compiler can assume that all changes to variables happen through pointers or references to variables of a type compatible to the accessed variable. Dereferencing a pointer that violates the aliasing rules results in undefined behavior and can be optimized out as follows.

Code Block
bgColor#FFCCCC

#include <stdio.h>

int main()
{
    short a\[2\];

    a[0]=0x1111;
    a[1]=0x1111;

    printf("%x %x\n", a[0], a[1]);
    return 0;
}

Wiki Markup
In the case above, the compiler may assume that no access through an integer pointer can change the array a, consisting of shorts. Thus, printf may be called with the original values of a\[0\] and a\[1\]. What really happens is up to the compiler and may change with architecture and optimization level.

...