Re: Unions Redux
- From: "Robert Gamble" <rgamble99@xxxxxxxxx>
- Date: 17 Mar 2007 21:50:35 -0700
On Mar 15, 5:25 am, CBFalconer <cbfalco...@xxxxxxxxx> wrote:
Jack Klein wrote:
... snip ...
There is no difference in aliasing in a union than there is via
pointer casting.
Not so. The compiler is able to insert conversion code to
implement a cast. It is not able to do so for an aliased union.
Which is why it is implementation or undefined behavior to access a
union component as other than the form in which it was stored.
To illustrate, consider a perverse 16 bit machine in which pointers
are machine addresses stored hi order byte first, and integers are
stored low order byte first. How is the compiler to know when to
flip the bytes when accessed through a union?
DR 283 addresses this by adding a footnote to 6.5.2.3#3 which states:
"If the member used to access the contents of a union object is not
the same as the member last used to store a value in the object, the
appropriate part of the object representation of the value is
reinterpreted as an object representation in the new type as described
in 6.2.6 (a process sometimes called "type punning"). This might be a
trap representation."
Robert Gamble
.
- References:
- Unions Redux
- From: Old Wolf
- Re: Unions Redux
- From: Jack Klein
- Re: Unions Redux
- From: CBFalconer
- Unions Redux
- Prev by Date: Re: K&R2 section 2.7 type conversions (exercise)
- Next by Date: Re: Requesting advice how to clean up C code for validating string represents integer
- Previous by thread: Re: Unions Redux
- Next by thread: Re: Unions Redux
- Index(es):
Relevant Pages
|