Re: Unions Redux



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

.



Relevant Pages

  • Re: Unions Redux
    ... pointer casting. ... The compiler is able to insert conversion code to ... It is not able to do so for an aliased union. ... stored low order byte first. ...
    (comp.lang.c)
  • Re: Unions, storage, ABIs
    ... > char Char; ... first address of the union. ... member. ... Even when compiled with a different compiler you could ...
    (comp.lang.c)
  • Re: COMMON structures and text parsing
    ... I was referring to the more generic comments above "THAT compiler that ... If that was the compiler that introduced TAB-format, UNION and %REF, ... of the Fortran 77 and some of the Fortran 90 improvements. ...
    (comp.lang.fortran)
  • Re: Copying from one struct to another, simple assignment?
    ... void set_char(char *ptr, char value) ... int main ... union u x; ... The compiler might zero the excess bytes of x when assigning '*' to ...
    (comp.lang.c)
  • Re: Returning a struct from a function - strange behavior
    ... preliminary draft for a future C1x standard. ... the struct or union is already too big to fit ... | course the compiler is still free to do the equivalent of storing it ... there isn't any need for added inefficiencies ...
    (comp.lang.c)