473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Union member access

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
Nov 14 '05 #1
3 2017
"Default User" <fi********@boeing.com.invalid> wrote in message
news:40***************@boeing.com.invalid...
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.

--
Peter
Nov 14 '05 #2
On Sat, 17 Jul 2004 12:12:18 +1000, "Peter Nilsson" <ai***@acay.com.au>
wrote:
"Default User" <fi********@boeing.com.invalid> wrote in message
news:40***************@boeing.com.invalid...

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.

Nov 14 '05 #3
Peter Nilsson wrote:

"Default User" <fi********@boeing.com.invalid> wrote in message
news:40***************@boeing.com.invalid...

[#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
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under...
2
by: Peter Dunker | last post by:
Hi, I will write ANSI C89. Is the following struct defenition correct ? I wrote it with VC6(Windows IDE) and at first no Problem. As I changed a compiler switch to 'no language extension', the...
73
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
18
by: ranjeet.gupta | last post by:
Dear ALL As we know that when we declare the union then we have the size of the union which is the size of the highest data type as in the below case the size should be 4 (For my case and...
14
by: Clint Olsen | last post by:
I was wondering if it's considered undefined behavior to use a member of a union when it wasn't initialized with that member. Example: typedef unsigned long hval_t; hval_t hval_init(void) {...
16
by: tedu | last post by:
does anyone know of a platform/compiler which will place union elements to not overlap? as in union u { int a; long b; size_t c; }; in my limited experience, writing to any of (a, b, or c)...
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
32
by: =?gb2312?B?zfWzrLey?= | last post by:
Union un { int I; char c; } main() { union un x; x.c=10; x.c=1;
2
by: chang | last post by:
Hi All, have some experience in C prog language. I have small doubt abt using unions in C language. Here is a small programm in vc++: union a{ int b; char c; }d;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.