From -662718750564581880
X-Google-Thread: f78e5,1130823ffefc6ebe
X-Google-Thread: fc772,1130823ffefc6ebe
X-Google-Attributes: gidf78e5,gidfc772,public
X-Google-Language: ENGLISH,ASCII
Path: g2news1.google.com!news1.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed-east.nntpserver.com!nntpserver.com!newsfeed1.sea.pnap.net!newsfeed2.sea.pnap.net!newsfeed.pnap.net!brmea-news-1.sun.com!news1nwk.sfbay.sun.com!taumet!clamage
From: kanze@gabi-soft.fr
Newsgroups: comp.std.c++,comp.lang.c++.moderated
Subject: Re: Unions up in arms!
Date: Mon, 13 Sep 2004 22:47:09 +0000 (UTC)
Organization: http://groups.google.com
Lines: 133
Sender: cppmods@netlab.cs.rpi.edu
Approved: stephen.clamage@sun.com (comp.std.c++)
Message-ID: <d6652001.0409130421.2c2cccc1@posting.google.com>
References: <9215d7ac.0409081832.4ab088d6@posting.google.com>
NNTP-Posting-Host: taumet.sfbay.sun.com
Mime-Version: 1.0
Content-Type: text/plain; charset=unknown-8bit
Content-Transfer-Encoding: QUOTED-PRINTABLE
X-Trace: news1nwk.SFbay.Sun.COM 1095115629 8388 129.146.79.208 (13 Sep 2004 22:47:09 GMT)
X-Complaints-To: usenet@news1nwk.sfbay.sun.com
NNTP-Posting-Date: Mon, 13 Sep 2004 22:47:09 +0000 (UTC)
X-MIME-Autoconverted: from 8bit to quoted-printable by nwkea-mail-1.sun.com id
 i8DLqb4d003549
X-Original-NNTP-posting-date: Mon, 13 Sep 2004 20:41:40 +0000 (UTC)
Delivered-to: std-c++@ucar.edu
X-Original-Date: Mon, 13 Sep 2004 05:22:02 -0700
X-Submission-Address: c++-submit@netlab.cs.rpi.edu
X-Auth: PGPMoose V1.1 PGP comp.lang.c++.moderated
 iQBVAwUAQUYGyEHMCo9UcraBAQEbRwIAoted+oXASqiQk+zc26Jq1LBEUE88nMsv
 GpF1x8lf0BWkZGVbtGXDZJPUnjeZlt1lJlXAH0aUtiAaPuSZ4fBQjQ==                =ITUL
X-Approved-For-Group: kuehl@inf.uni-konstanz.de comp.lang.c++.moderated
X-NNTP-posting-host: netlab.cs.rpi.edu
Original-recipient: rfc822;clamage@mpk-mail1.SFBay.Sun.COM
Originator: clamage@taumet
Xref: g2news1.google.com comp.std.c++:2601 comp.lang.c++.moderated:8137


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

> Color me stupid, but after months of staring I still can't grok thi=
s
> clause in the Standard 3.10/15:

> ----

> If a program attempts to access the stored value of an object throu=
gh
> an lvalue of other than one of the following types the behavior is
> undefined:

> an aggregate or union type that includes one of the aforementioned
> types among its members (including, recursively, a member of a
> subaggregate or contained union),

> ----

The wording is a bit difficult, to put it mildly.  I think that the k=
ey
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.

For the rest, there are a lot of other rules which have to be taken i=
nto
account.  Thus, for example, *(float*)ps is definitly not a legal
access, despite the text you quote.

> Suppose

> int j;
> union u { int i; float f; };

> Then which of the following is illegal, if any, and why? (Putting
> aside, if possible, the legality of the popular type-punning practi=
ce
> of putting x into a union and getting back y.)

But you can't put it aside.  The standard clearly says that the only
member of a union that can be accessed is the last one written.

> A.

> j =3D 12;
> int k =3D ((u&) j).i;

This is a reinterpret_cast.  According to =A75.2.10/7, "Except that
converting an rvalue of type 'pointer to T1' to the type 'pointer to
T2' (where T1 and T2 are object types and where the alignement
requirements of T2 are no stricter than those of T1) and back to its
original type yields the original pointer value, the result of such a
pointer conversion is unspecified."  So there results here are
unspecified.

Note that even if you converted back to the original type, if float
required stricter alignment (or if the compiler imposed stricter
alignment on unions than on its constituant members), the results wou=
ld
be unspecified.

> B.

> j =3D 12;
> u v =3D (u&) j;

Unspecified, see above.

> C.

> u v =3D {12};
> float g =3D v.f;

The union was initialized with an int.  You access the float field.
Undefined behavior.  On machines using IEEE, this can cause a trappin=
g
NaN.  On machines where floats are larger than ints, who knows what m=
ay
happen.  In this case, it doesn't even work in practice.

> D.

> j =3D 12;
> float g =3D ((u&) j).f;

You're using an unspecified pointer to access a field in a union that
doesn't exist.  What do you expect to happen.

> And finally, what does "including, recursively, a member of a
> subaggregate or contained union" mean?

It means that if you have a float*, and a U*, for some union or
aggregate, if that union or aggregate contains a float, directly, or
indirectly, as a member of one of its members, then the compiler must
consider that the the two pointers may possibly be aliases for the sa=
me
data, i.e. if there is a write through one of the pointers, the compi=
ler
must reread any value read through the other, since this write may ha=
ve
modified the value.

--
James Kanze           GABI Software         http://www.gabi-soft.fr
Conseils en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, 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 wi=
th ]
[ 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                    =
   ]



