The may_alias type attribute
You can specify the may_alias type attribute for a type so that lvalues of the type can alias objects of any type, similar to a char type. Types with the may_alias attribute are not subject to type-based aliasing rules.
struct __attribute__((__may_alias__)) my_struct {} *ps;
typedef long __attribute__((__may_alias__)) t_long;
typedef struct __attribute__((__may_alias__)) my_struct {} t_my_struct;
#define __attribute__(x) // Invalidates all __attribute__ declarations
typedef float __attribute__((__may_alias__)) t_float;
int main (void){
int i = 42;
t_float *pa = (t_float *) &i;
*pa = 0;
if (i == 42)
return 1;
return 0;
}
If you compile this code with the -fstrict-aliasing (-qalias=ansi) option at a high optimization level,
such as -O3,
the executable program returns 1. Because the lvalue *pa is
of type float, according to the ANSI aliasing rules,
the assignment to lvalue *pa cannot modify the value
of i, which is of type int.If you remove the #define __attribute__(x) statement and compile the code with the same options as before, the executable program returns 0. Because the type of *pa is float __attribute__((__may_alias__)), *pa can alias any other object of any type, and the assignment to lvalue *pa can modify the value of i to 0.
The use of the may_alias type attribute can result in less pessimistic aliasing assumptions by the compiler, leading to more optimization opportunities, compared to usage of the -fno-strict-aliasing (-qalias=noansi) compiler option.
This attribute is supported at the extc89, extc99, extended, and extc1x language levels.
This attribute is supported at the extended, extended0x, and extended1y language levels.