From 521079288950720281
X-Google-Thread: fc772,1130823ffefc6ebe
X-Google-Thread: f78e5,1130823ffefc6ebe
X-Google-Attributes: gidfc772,gidf78e5,public
X-Google-Language: ENGLISH,ASCII
Path: g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed-east.nntpserver.com!nntpserver.com!newsfeed1.sea.pnap.net!newsfeed.pnap.net!brmea-news-1.sun.com!news1nwk.sfbay.sun.com!taumet!clamage
From: kanze@gabi-soft.fr
Newsgroups: comp.lang.c++.moderated,comp.std.c++
Subject: Re: Unions up in arms!
Date: Fri, 17 Sep 2004 15:29:56 +0000 (UTC)
Organization: http://groups.google.com
Lines: 67
Sender: cppmods@netlab.cs.rpi.edu
Approved: stephen.clamage@sun.com (comp.std.c++)
Message-ID: <d6652001.0409152358.648f6065@posting.google.com>
References: <9215d7ac.0409081832.4ab088d6@posting.google.com>
 <d6652001.0409130421.2c2cccc1@posting.google.com>
 <9215d7ac.0409140039.727afd2f@posting.google.com>
NNTP-Posting-Host: taumet.sfbay.sun.com
Mime-Version: 1.0
Content-Type: TEXT/PLAIN
Content-Transfer-Encoding: 8BIT
X-Trace: news1nwk.SFbay.Sun.COM 1095434996 3332 129.146.79.208 (17 Sep 2004 15:29:56 GMT)
X-Complaints-To: usenet@news1nwk.sfbay.sun.com
NNTP-Posting-Date: Fri, 17 Sep 2004 15:29:56 +0000 (UTC)
X-Original-NNTP-posting-date: Fri, 17 Sep 2004 02:43:34 +0000 (UTC)
Delivered-to: std-c++@ucar.edu
X-Original-Date: Thu, 16 Sep 2004 00:58:33 -0700
X-Submission-Address: c++-submit@netlab.cs.rpi.edu
X-Auth: PGPMoose V1.1 PGP comp.lang.c++.moderated
 iQBVAwUAQUpQHkHMCo9UcraBAQFzIQH+OS/ILsdpzA1Xj3ndXfVT4WSFWQHelSiD
 liLi1Xwd66JZM9Bs9ERXn8wfmPcY5M5TtU6uOz54SuTe5vDXRkAt4A==                =kBD5
X-Approved-For-Group: kuehl@inf.uni-konstanz.de comp.lang.c++.moderated
X-NNTP-posting-host: netlab.cs.rpi.edu
Original-recipient: rfc822;stephen.clamage@sun.com
Originator: clamage@taumet
Xref: g2news1.google.com comp.lang.c++.moderated:8428 comp.std.c++:2694


glenlow@pixelglow.com (Glen Low) wrote in message
news:<9215d7ac.0409140039.727afd2f@posting.google.com>...

> > The wording is a bit difficult, to put it mildly. I think that the
> > key is in the (non-normative) footnote. These restrictions are
> > basicly there to tell compiler writers what they can exclude (or
> > rather what they cannot exclude) as possible aliases. Thus, if I
> > have

> >     strut S { int i ; float f ; } ;
> >     float ff ;
> > 
> >     S* ps ;
> >     float* pf ;

> > The compiler must assume that *pf is also accessible via ps.

> Hewing close to the language of the Standard, if I store into *pf, the
> compiler must assume that a read from *ps would be affected.

Correct.

> The question is, are the following reads also affected: ps->i, ps->f ?

Good question.  ps->f is definitly affected.  Formally, I don't think
that the compiler is obliged to take into account ps->i -- reading from
ps->i is undefined behavior if you've written to ps->f, and if *pf
wasn't an alias for ps->f, ps->i shouldn't change.

In practice, I would consider it a poor compiler which didn't support
this.  Correct or not, I think it is a common idiom.  And although you
do loose an optimization possibility, you only loose it in the presence
of a union, which shouldn't be that frequent.  (FWIW: at least one early
C compiler I used, Microsoft C 1.0, didn't support it -- writing into
one field of a union, and reading from another often didn't work.)

> Note that *ps has an lvalue of union type and so the above analysis is
> correct, but how about ps->f which has an lvalue of float type? Does
> "through an lvalue" mean "an lvalue expression of this exact type" or
> "an lvalue expression that contains this type in it e.g. as part of a
> member access"?

> For the sake of argument, let's assume that sizeof (int) == sizeof
> (float) and the data in question is a valid sequence of bits for
> either int or float.

Both of which are irrelevant with regards to the current discussion.
The standard says that reading from  any element other than the last one
written is undefined behavior.  It doesn't say why, and the compiler is
free to assume that your code doesn't do it.

--
James Kanze           GABI Software         http://www.gabi-soft.fr
Conseils en informatique orient�e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]



