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