By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,367 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,367 IT Pros & Developers. It's quick & easy.

union usage

P: n/a
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

Dec 27 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
es*****@acm-dot-org.invalid
Dec 27 '05 #2

P: n/a
i also think it works right

Dec 28 '05 #3

P: n/a
j0mbolar <j0******@engineer.com> 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 si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Dec 28 '05 #4

P: n/a
S.Tobias wrote:
j0mbolar <j0******@engineer.com> 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.
Dec 28 '05 #5

P: n/a
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
S.Tobias wrote:
j0mbolar <j0******@engineer.com> 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 si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Dec 28 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.