🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #4187

Re: Union access

#4187
From: Dan Pop
Date: Thu, 05 Oct 1995 18:02
31 lines
1442 bytes
In <812736828snz@soliton.demon.co.uk> Ian Cargill <ian@soliton.demon.co.uk> writes:

>It has been asserted elsewhere by someone that:
>
>> "The only guarantee that C gives is that the following data 
>>  structure can be written one way and read another."  
>>
>>   union {
>>     unsigned char a[4];
>>     int b;
>>     }
>
>I replied with my opinion that this is actually implementation defined 
>behavior, and that the only guarantee the standard gives is regarding 
>access to common initial sequences of structs in a union.

If you write it one way and read it another you invoke undefined 
behaviour because you read an indeterminately-valued object.  ANSI 3.5.2.1:

    The value of at most one of the members can be stored in a
    union object at any time.

Hence, once you initialize one member, the values of all the others become
indeterminate.

Dan
--
Dan Pop
CERN, CN Division
Email: Dan.Pop@mail.cern.ch 
Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
Technical Details
Message-ID: <danpop.812916141@rscernix>
Path: arc1-ca.pugleaf.net!CERN.ch!rscernix!danpop
References: <812736828snz@soliton.demon.co.uk>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!CERN.ch!rscernix!danpop
From: Dan.Pop@mail.cern.ch (Dan Pop)
Subject: Re: Union access
X-Nntp-Posting-Host: ues5.cern.ch
Message-ID: <danpop.812916141@rscernix>
Sender: news@news.cern.ch (USENET News System)
Organization: CERN European Lab for Particle Physics
X-Newsreader: NN version 6.5.0 #7 (NOV)
References: <812736828snz@soliton.demon.co.uk>
Date: Thu, 5 Oct 1995 18:02:21 GMT