Differences between -qalias=ansi and -qalias=typeptr
FangLu 2700058AGP Visits (1417)
The IBM XL C and XL C/C++ compilers support the -qalias option. This option indicates whether a program contains certain categories of aliasing or does not conform to C/C++ standard aliasing rules. The -qalias option takes several suboptions, including ansi and typeptr.
When ansi is in effect, type-based aliasing is used during optimization; that is, pointers can only point to an object of the same type. The only exception is that a char* dereference can alias any other types.
typeptr complies to more restrictive aliasing rules than ansi. When typeptr is in effect, pointers to different types are never aliased; that is, pointers can only point to an object of the same type and even a char* dereference cannot alias any other types.
See the following example.
char *pch = (char*)&i;
*pch = 9;
To compile a.cpp under ansi, you must specify an optimization option to make ansi effective:
xlC a.cpp -O4 -qalias=ansi
The output is 55, which demonstrates that the char* dereference *pch under ansi can access the memory of the integer i.
To compile a.cpp under typeptr, you must specify an optimization option and ansi to make typeptr effective:
xlC a.cpp -O4 -qalias=ansi -qalias=typeptr
The output is 66, which demonstrates that the char* dereference *pch under typeptr cannot access the memory of the integer i.