Union member access

D

Default User

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
 
P

Peter Nilsson

Default User said:
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.
 
M

Mathew Hendry

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.
 
D

Default User

Peter said:
[#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
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top