...
Recent versions of GCC turn on the option -fstrict-aliasing
(,
which allows alias-based optimizations) , by default with -O2
. Some architectures then print "1111 1111" as a result. Without optimization, the executable generates the expected output "2222 2222."
To disable optimizations based on alias analysis for faulty legacy code, the option -fno-strict-aliasing
can be used as a workaround. The option -Wstrict-aliasing
(,
which is included in -Wall
) ,
warns about some, but not all, violations of aliasing rules when -fstrict-aliasing
is active.
...
The printf()
behavior in this compliant solution is unspecified, but it is commonly accepted as an implementation extension. (see See unspecified behavior 11.).
This function typically outputs "2222 2222." However, there is no guarantee that this will be true, even on implementations that defined the unspecified behavior; values of type short
need not have the same representation as values of type int
.
...
According to the C Standard, 6.7.6.2 [ISO/IEC 9899:2011], using two or more incompatible arrays in an expression is undefined behavior. (see See also undefined behavior 76.).
For two array types to be compatible, both should have compatible underlying element types, and both size specifiers should have the same constant value. If either of these properties is violated, the resulting behavior is undefined.
...