Proposed resolution to aliasing issue.

Mark Mitchell mark@codesourcery.com
Tue May 17 21:20:00 GMT 2005


Nathan Sidwell wrote:

> I attended a UK C++ panel meeting yesterday, and took the opportunity
> to solicit opinions on this.  The question I posed was
>     struct A {
>             ...
>         T1 a;
>         T2 b;
>     };
>     void g(T1 &a);
>     void Foo () {
>        A v;
>        v.b = 2;
>        g (v.a);
>        if (v.b == 2) ...
>         }
> Does the compiler have a right to presume v.b does indeed == 2 in the if
> condition? -- assuming T2 is a suitable type for that :)

> After I explained the optimization (and the related structure splitting
> optimization), the general consensus was 'yes that would be a useful
> optimization'.  But no one was sufficiently confident of proving it
> was allowable.  The opinion was expressed that if it was not allowable,
> there was a bug in the std.

Will the UK committee open a DR for this?  Or, would you care to send 
mail to Steve Adamczyk about it?

> The observation was made that if A is non-POD, one cannot play offsetof
> tricks to get from A::a to A::b, so the optimization is safe on non-PODs.
> (Of course one would have to prove the address of 'v' did not escape,
> so I guess the ctor and dtor would need to be trivial or visible.)

I argued last week that this was not in fact true, in that you can do:

   ptrdiff_t x = &v.b - &v.a;

and then use that instead of "offsetof (Foo, b) - offsetof (Foo, a)".

> because (a) badfuncs are more than likely rare and (b) it would be a useful
> aid to the programmer.[1] Mark outlines an __I_AM_GOOD__ attribute,  I 
> think
> it would be better to have both flavours and then the compiler switch can
> specify which way the default goes.

Makes sense to me.

-- 
Mark Mitchell
CodeSourcery, LLC
mark@codesourcery.com
(916) 791-8304



More information about the Gcc mailing list