How to use __attribute__((__may_alias__))

John Fine johnsfine@verizon.net
Tue Mar 17 21:46:00 GMT 2009


I'm using gcc 4.3.2 x86_64

I'm trying to work around consequences of a reinterpret_cast.  I 
verified that compiling with -fno-strict-aliasing fixes the problem, but 
I don't want to do that to the whole compilation unit.  I want to limit 
the change to the one function with the problem.

Sorry I haven't reduced this to an example small enough to post.  I'm 
hoping someone know how to use __attribute__((__may_alias__)) well 
enough to answer this from just the two lines of code exhibiting the 
problem.

DAT* pp=foo();
reinterpret_cast<DAT_PTR*>(&pp)->bar(x);

DAT is a class.
DAT_PTR is the class of an object that is the size of a DAT* and the 
contents of a DAT* and acts almost like a DAT* but not exactly.
Constructing or destroying an actual DAT_PTR is impractical at that 
point in the code.  Making a DAT* and pretending it is a DAT_PTR fits 
the situation perfectly.

But the compiler optimizes away the pp=foo() because it sees pp is never 
used as itself.

I tried
DAT __attribute__((__may_alias__))* pp=foo();
and
DAT* __attribute__((__may_alias__)) pp=foo();
but neither of those make any difference.

I've solved problems like this in the past with unions, but I'd prefer 
not to in this case.  I though  __attribute__((__may_alias__)) was 
supposed to deal with this issue.

I'd like to know how to use that in general for a few other cases (in a 
very big collection of old code) where I expect to need it.




More information about the Gcc-help mailing list