From -543141276145967686
X-Google-Thread: f78e5,1130823ffefc6ebe
X-Google-Attributes: gidf78e5,public
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news1.google.com!news1.google.com!news.glorb.com!news.zanker.org!news.clara.net!wagner.news.clara.net!194.159.246.34.MISMATCH!peer-uk.news.demon.net!kibo.news.demon.net!news.demon.co.uk!demon!stump.algebra.com!devnull
From: swelef@post.sk (Vladimir Marko)
Newsgroups: comp.std.c++
Subject: Re: Unions up in arms!
Date: Tue, 14 Sep 2004 17:37:18 GMT
Organization: http://groups.google.com
Lines: 89
Sender: mail2news@demon.net
Approved: fjh@cs.mu.oz.au (Fergus Henderson , moderator of comp.std.c++)
Message-ID: <84523212.0409140907.46471b77@posting.google.com>
References: <9215d7ac.0409081832.4ab088d6@posting.google.com> <d6652001.0409130421.2c2cccc1@posting.google.com>
NNTP-Posting-Host: news.news.demon.net
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: news.demon.co.uk 1095183443 20280 158.152.254.254 (14 Sep 2004 17:37:23 GMT)
X-Complaints-To: abuse@demon.net
NNTP-Posting-Date: Tue, 14 Sep 2004 17:37:23 +0000 (UTC)
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Spam-Checker-Version: SpamAssassin 2.64-mulga_r1 (2004-01-11) on 
	mulga.cs.mu.OZ.AU
X-Spam-Status: No, hits=-4.9 required=5.0 tests=BAYES_00 autolearn=ham 
	version=2.64-mulga_r1
X-Received: (from fjh@localhost)
	by mulga.cs.mu.OZ.AU (8.12.10+Sun/8.12.9/Submit) id i8EHbItt000523;
	Wed, 15 Sep 2004 03:37:18 +1000 (EST)
X-Path: comp-std-cpp-robomod!not-for-mail
X-NNTP-Posting-Date: Tue, 14 Sep 2004 17:07:35 +0000 (UTC)
X-Spam-Level: 
X-Delivered-To: std-c++@ucar.edu
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Newsgroups: comp.std.c++
Xref: g2news1.google.com comp.std.c++:2618

kanze@gabi-soft.fr wrote in message news:<d6652001.0409130421.2c2cccc1@posting.google.com>...
> glenlow@pixelglow.com (Glen Low) wrote in message
> news:<9215d7ac.0409081832.4ab088d6@posting.google.com>...
[snip]
> 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
> into account.  Thus, for example, *(float*)ps is definitly not
> a legal access, despite the text you quote.

Oh, really? First, we have 9.2/17:
  A pointer to a POD-struct object, suitably converted using a
  reinterpret_cast, points to its initial member (or if that member
  is a bit-field, then to the unit in which it resides) and vice versa.
Thus, *(int*)ps is surely valid. Then, 9.5/1 says
  [snip] Each data member is allocated as if it were the sole member of
  a struct. [snip]
It comes down to the question "How deep is that 'as if'?" Is it deep
enough for the 9.2/17 to apply? Given the exact wording, I'd say no
(i.e. you are right). On the other hand, making it behave wrong would
require a special handling of this case in the implementation's
reinterpret_cast. AFAICT the standard could be changed to allow it
without any implications for the vendors.

> > 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 = 12;
> > int k = ((u&) j).i;
> 
> This is a reinterpret cast.  According to  5.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.

But _if the union has no stricter alignment requirements_, the
following is well defined:
  u* pu=(u*)&j;
  int k=*(int*)pu;
And in the light of 9.2/17, the last line is pretty much equivalent to
  int k=pu->i;
Casting references instead of pointers gives the same behaviour,
thus the original example could be valid _if the union had no stricter
alignment requirements_, couldn't it? Well, "pretty much equivalent"
is not the same as "equivalent." 9.2/17 speaks about converting
a pointer to POD struct and pu is _not_ a pointer to an POD struct,
it is just a correctly typed pointer "somewhere." So, it's again
something that should work with a reasonable implementation while
beeing marked as undefined behaviour by the standard. This time,
however, even if the standard could be changed to accept this, it
should _not_ be changed.

Vladimir Marko

---
[ 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                       ]



