Clarification on Gcc's strict aliasing rules

Francis Moreau francis.moro@gmail.com
Sat Nov 6 22:15:00 GMT 2010


Hello,

I'm trying to understand what '-fstrict-aliasing' actually means.

Looking at the man page, one can read:

   -fstrict-aliasing
       Allow the compiler to assume the strictest aliasing
       rules applicable to the language being compiled.  For C
       [...]

What does "the strictest aliasing rules ..." means ?

   - Does it mean that Gcc allows only a subset of the standard aliasing
     rules (which I believe is defined in 6.5p7 ? In this case, which
     ones ?
     That would mean that Gcc also miss compiled conforming programs.

   - Does it mean that the C standard has some 'gray' areas (undefined
     behaviours) about aliasing and Gcc choose to specify the behaviour
     for optimisation purposes ?

   - or something else ?


Also I don't really understand the examples provided by the man page. Ok
type punning using union is still allowed.

But the second example which is:

    However, this code might not:

		  int f() {
		    union a_union t;
		    int* ip;
		    t.d = 3.0;
		    ip = &t.i;
		    return *ip;
		  }

_might_ not work. Why using 'might' ? Does it work or not ?
AFAIK, this type of aliasing is defined by the standard.

The third example is:

    Similarly, access by taking the address, casting the
    resulting pointer and dereferencing the result has
    undefined behavior, even if the cast uses a union type,
    e.g.:

		  int f() {
		    double d = 3.0;
		    return ((union a_union *) &d)->i;
		  }

Again in my understanding of the standard, this is an undefined
behaviour. So why does man page mention this case ?

Clarifications would be very appreciated.

Thanks.
-- 
Francis



More information about the Gcc-help mailing list