Union member access

Discussion in 'C Programming' started by Default User, Jul 16, 2004.

  1. Default User

    Default User Guest

    After a discussion on c.l.c++ with Ron Natalie, I'm now unclear as to
    the status of reading data stored as one union member, then accessed
    with a different one, in C99.

    I don't have the latest standard, as I don't work in it currently, so I
    use the draft standard.

    My reading was that this is implementation-defined, based on this:

    6.5.2.2
    [#5] With one exception, if the value of a member of a union
    object is used when the most recent store to the object was
    to a different member, the behavior is
    implementation-defined.70)


    Ron Natalie states that it's usually undefined behaviro, according to
    the following:

    6.5
    [#7] An object shall have its stored value accessed only by
    an lvalue expression that has one of the following types:63)

    -- a type compatible with the effective type of the
    object,

    -- a qualified version of a type compatible with the
    effective type of the object,

    -- a type that is the signed or unsigned type
    corresponding to the effective type of the object,

    -- a type that is the signed or unsigned type
    corresponding to a qualified version of the effective
    type of the object,

    -- 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), or

    -- a character type.



    So that unless the other union member meets these qualifications, it's
    UB. Is this correct? Was 6.5.2.2 [#5] modified, perhaps?



    Brian Rodenborn
     
    Default User, Jul 16, 2004
    #1
    1. Advertising

  2. "Default User" <> wrote in message
    news:...
    > After a discussion on c.l.c++ with Ron Natalie, I'm now unclear as to
    > the status of reading data stored as one union member, then accessed
    > with a different one, in C99.
    >
    > I don't have the latest standard, as I don't work in it currently, so I
    > use the draft standard.
    >
    > My reading was that this is implementation-defined, based on this:
    >
    > 6.5.2.2


    ITYM 6.5.2.3

    > [#5] With one exception, if the value of a member of a union
    > object is used when the most recent store to the object was
    > to a different member, the behavior is
    > implementation-defined.70)


    That sentence was removed from the published C99 standard.

    --
    Peter
     
    Peter Nilsson, Jul 17, 2004
    #2
    1. Advertising

  3. On Sat, 17 Jul 2004 12:12:18 +1000, "Peter Nilsson" <>
    wrote:

    >"Default User" <> wrote in message
    >news:...
    >
    >ITYM 6.5.2.3
    >
    >> [#5] With one exception, if the value of a member of a union
    >> object is used when the most recent store to the object was
    >> to a different member, the behavior is
    >> implementation-defined.70)

    >
    >That sentence was removed from the published C99 standard.


    It does say (6.2.6.1)

    |7 When a value is stored in a member of an object of union type, the
    |bytes of the object representation that do not correspond to that
    |member but do correspond to other members take unspecified values,
    |but the value of the union object shall not thereby become a trap
    |representation.

    which somehow becomes in Annex J

    |J.1 Unspecified behavior
    |1 The following are unspecified:
    |...
    |— The value of a union member other than the last one stored into
    |(6.2.6.1)

    That may be the intent, but Annex J is non-normative, and I don't see how
    one gets from one to the other...

    -- Mat.
     
    Mathew Hendry, Jul 17, 2004
    #3
  4. Default User

    Default User Guest

    Peter Nilsson wrote:
    >
    > "Default User" <> wrote in message
    > news:...


    > > [#5] With one exception, if the value of a member of a union
    > > object is used when the most recent store to the object was
    > > to a different member, the behavior is
    > > implementation-defined.70)

    >
    > That sentence was removed from the published C99 standard.



    Ok, thanks.



    Brian Rodenborn
     
    Default User, Jul 19, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Garman
    Replies:
    1
    Views:
    677
    Matt Garman
    Apr 25, 2004
  2. Jeff Massung

    Setting union member in structure

    Jeff Massung, Dec 22, 2003, in forum: C++
    Replies:
    2
    Views:
    484
    Jeff Massung
    Dec 22, 2003
  3. Peter Dunker

    union in struct without union name

    Peter Dunker, Apr 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    896
    Chris Torek
    Apr 26, 2004
  4. Michael B Allen

    Anonymous Union Member Access

    Michael B Allen, Sep 3, 2004, in forum: C Programming
    Replies:
    1
    Views:
    468
    xarax
    Sep 3, 2004
  5. S.Tobias

    access union member via ptr

    S.Tobias, Oct 28, 2004, in forum: C Programming
    Replies:
    0
    Views:
    360
    S.Tobias
    Oct 28, 2004
Loading...

Share This Page