union usage

J

j0mbolar

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?
 
E

Eric Sosman

j0mbolar said:
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.
 
S

S.Tobias

j0mbolar said:
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)?
 
F

Flash Gordon

S.Tobias said:
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.
 
S

S.Tobias

Flash Gordon said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top