union usage

Discussion in 'C Programming' started by j0mbolar, Dec 27, 2005.

  1. j0mbolar

    j0mbolar Guest

    The following was labeled as invoking undefined behavior
    but I don't see that happening at all:

    struct ints {
    int a;
    int b;
    };

    union merge {
    float f;
    struct ints typ;
    };

    union merge i;
    union merge j;

    i.f = 1;
    j = i; /* means j.f = 1 ? */

    i.typ.a = i.typ.b = 0; /* we assign here, we are not reading the value
    of i.type.b */
    j = i; /* means j.typ.a = j.typ.b = 0 ? */


    This is all valid, right?

    --
    j0mbolar
     
    j0mbolar, Dec 27, 2005
    #1
    1. Advertising

  2. j0mbolar

    Eric Sosman Guest

    j0mbolar wrote:
    > The following was labeled as invoking undefined behavior
    > but I don't see that happening at all:
    >
    > struct ints {
    > int a;
    > int b;
    > };
    >
    > union merge {
    > float f;
    > struct ints typ;
    > };
    >
    > union merge i;
    > union merge j;
    >
    > i.f = 1;
    > j = i; /* means j.f = 1 ? */
    >
    > i.typ.a = i.typ.b = 0; /* we assign here, we are not reading the value
    > of i.type.b */
    > j = i; /* means j.typ.a = j.typ.b = 0 ? */
    >
    >
    > This is all valid, right?


    Looks all right to me.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Dec 27, 2005
    #2
    1. Advertising

  3. j0mbolar

    Guest

    i also think it works right
     
    , Dec 28, 2005
    #3
  4. j0mbolar

    S.Tobias Guest

    j0mbolar <> wrote:

    > The following was labeled as invoking undefined behavior
    > but I don't see that happening at all:
    >
    > struct ints {
    > int a;
    > int b;
    > };
    >
    > union merge {
    > float f;
    > struct ints typ;
    > };
    >
    > union merge i;
    > union merge j;
    >
    > i.f = 1;
    > j = i; /* means j.f = 1 ? */
    >
    > i.typ.a = i.typ.b = 0; /* we assign here, we are not reading the value
    > of i.type.b */


    Isn't the value of the object `i.typ' in the above line modified twice
    without an intervening sequence point (cf. 6.5#2)?

    > j = i; /* means j.typ.a = j.typ.b = 0 ? */
    >
    >
    > This is all valid, right?


    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
     
    S.Tobias, Dec 28, 2005
    #4
  5. j0mbolar

    Flash Gordon Guest

    S.Tobias wrote:
    > j0mbolar <> wrote:
    >
    >> The following was labeled as invoking undefined behavior
    >> but I don't see that happening at all:
    >>
    >> struct ints {
    >> int a;
    >> int b;
    >> };
    >>
    >> union merge {
    >> float f;
    >> struct ints typ;
    >> };
    >>
    >> union merge i;
    >> union merge j;
    >>
    >> i.f = 1;
    >> j = i; /* means j.f = 1 ? */
    >>
    >> i.typ.a = i.typ.b = 0; /* we assign here, we are not reading the value
    >> of i.type.b */

    >
    > Isn't the value of the object `i.typ' in the above line modified twice
    > without an intervening sequence point (cf. 6.5#2)?


    Since you are modifying different fields within the struct, I can't see
    that that would apply.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Dec 28, 2005
    #5
  6. j0mbolar

    S.Tobias Guest

    Flash Gordon <> wrote:
    > S.Tobias wrote:
    >> j0mbolar <> wrote:
    >>
    >>> The following was labeled as invoking undefined behavior
    >>> but I don't see that happening at all:
    >>>
    >>> struct ints {
    >>> int a;
    >>> int b;
    >>> };
    >>>
    >>> union merge {
    >>> float f;
    >>> struct ints typ;
    >>> };
    >>>
    >>> union merge i;
    >>> union merge j;
    >>>
    >>> i.f = 1;
    >>> j = i; /* means j.f = 1 ? */
    >>>
    >>> i.typ.a = i.typ.b = 0; /* we assign here, we are not reading the value
    >>> of i.type.b */

    >>
    >> Isn't the value of the object `i.typ' in the above line modified twice
    >> without an intervening sequence point (cf. 6.5#2)?

    >
    > Since you are modifying different fields within the struct, I can't see
    > that that would apply.


    [blushing] Right, I'm sorry, I thought `i.typ' was a union; must buy glasses.

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
     
    S.Tobias, Dec 28, 2005
    #6
    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. metfan
    Replies:
    2
    Views:
    4,887
    Robert Olofsson
    Oct 21, 2003
  2. Matt Garman
    Replies:
    1
    Views:
    692
    Matt Garman
    Apr 25, 2004
  3. Peter Dunker

    union in struct without union name

    Peter Dunker, Apr 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    929
    Chris Torek
    Apr 26, 2004
  4. Colin J. Williams

    Webchecker Usage - a problem with local usage

    Colin J. Williams, Feb 25, 2004, in forum: Python
    Replies:
    1
    Views:
    575
    Colin J. Williams
    Feb 26, 2004
  5. hvt
    Replies:
    0
    Views:
    1,240
Loading...

Share This Page