Type-based aliasing

The compiler follows the type-based aliasing rule in the C standard when the -qalias=ansi option is in effect (which it is by default). This rule, also known as the ANSI aliasing rule, states that a pointer can only be dereferenced to an object of the same type or a compatible type. 1

The common coding practice of casting a pointer to an incompatible type and then dereferencing it violates this rule. (Note that char pointers are an exception to this rule)

The compiler uses the type-based aliasing information to perform optimizations to the generated code. Contravening the type-based aliasing rule can lead to unexpected behavior, as demonstrated in the following example:
   int *p;
   double d = 0.0;

   int *faa(double *g);         /* cast operator inside the function   */

   void foo(double f) {
       p = faa(&f);             /* turning &f into an int ptr     */
       f += 1.0;                /* The optimizer might move the  */
                                /* assignment after the printf statement. */
       printf("f=%x\n", *p);
   }

   int *faa(double *g) { return (int*) g; }   /* questionable cast;       */
                                              /* the function can be in   */
                                              /* another translation unit */

   int main() {
          foo(d);
    }
In the above printf statement, *p cannot be dereferenced to a double under the ANSI aliasing rule. The compiler determines that the result of f += 1.0 does not affect the value of *p. Thus, the optimizer might move the assignment after the printf statement. If you compile the above example with optimization enabled, the printf statement might output 0 (zero).
1 The C Standard states that an object shall have its stored value accessed only by an lvalue that has one of the following types:


Voice your opinion on getting help information Ask IBM compiler experts a technical question in the IBM XL compilers forum Reach out to us