Sergei Organov <[EMAIL PROTECTED]> writes: > Below are two example functions foo() and boo(), that I think both are > valid from the POV of strict aliasing rules. GCC 4.2 either warns about > both (with -Wstrict-aliasing=2) or doesn't warn about any (with > -Wstrict-aliasing), and generates the assembly as if the functions don't > violate the rules, i.e, both functions return 10.
-Wstrict-aliasing=2 is documented to return false positives. Actually both current versions of -Wstrict-aliasing are pretty bad. > $ cat alias.c > typedef struct { int i; } S; > > int i; > int foo() > { > S const sc = { 10 }; > i = 20; > // Accessing object 'i' of type 'int' through 'S' containing 'int' > // field. Should be OK from C99 standard POV? > *(S*)&i = sc; C99 says that you can access an object via "an aggregate or union type that includes one of the aforementioned [basically, compatible] types among its members (including, recursively, a member of a subaggregate or contained union)" (section 6.5, paragraph 7). So on that basis this looks OK to me. > S s; > int boo() > { > s.i = 20; > // Accessing 's' of type 'S' through 'int'. Is it aliasing rules > // violation? Maybe yes, but on the other hand this could be > // considered as accessing 's.i' of type 'int' through 'int' that > // should be OK from C99 standard POV? > *(int*)&s = 10; > return s.i; > } I think this should also be OK. The main point is that you are accessing the object with the correct type (int). The use of the struct wrapper does not change that. If you used a struct for which the type of the field was not the same as the type of the variable, then this usage would be an aliasing violation. It is possible that somebody else will disagree with me. Ian