This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Clarification on Gcc's strict aliasing rules


On 11/15/2010 01:45 AM, Francis Moreau wrote:
Andrew Haley<aph@redhat.com> writes:

On 11/12/2010 07:45 PM, Francis Moreau wrote:
"Segher Boessenkool"<segher@kernel.crashing.org> writes:

But you finally said
     - you can not access it as int:

         that object (t.i) does not have a stored value therefore it
         doesn't exist.
(Your words, not mine -- and such sloppy wording gets you into trouble,
the standard does not talk about any of this.  It is one way of looking
at it though).
So what did you mean by this ?

> could you tell me what the effective type of 't.i' object ?

    int, if you can say that object exists at all: it does not have a stored
    value.  The stored value of t is a double with value 3.0 .  You can
    take its address and access it via that as "double" (or "char"), or you
    can access it as the union it is.  You can not access it as "int".

This is what I understood from what you said, please correct me if I'm
wrong.

However doing:

int i = t.i;

is defined in C (as long as there's no trap representation) even if 't.i'
object has no stored value.
Actually, I think this is a GCC extension, and I was mistaken to say it
is valid C99 before.  Standard C allows you to read from t.d or t, but not
t.i, after storing into t.d .
No.

    t.d = 3.0;
    i = t.i;

is well defined in C.

Again, what's ambiguous is the example given by the GCC man:

    int *ip;
    t.d = 3.0;
    ip =&t.i;
    return *ip;

which produces code that might or not work.

6.5p7 lists this as a possible alias case and I can't find any rule in
the standard that could invalidate it.

So either GCC is not conformant in this regard or I'm missing something.
The thing you're missing is that alias case doesn't mean ok. It actually means, in this case not ok. alias rules aren't to tell you how to do type-punning, but to tell compiler writers when it's safe to do some classes of optimization. If you break the rules by hiding your aliasing it's undefined (and often unpleasant) what might happen. If gcc notices the type-punning that breaks the rules they are nice enough to warn you that when optimization happens you might be unhappy.

Patrick


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]