This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Undefined behavior due to 6.5.16.1p3
- From: Vincent Lefevre <vincent+gcc at vinc17 dot org>
- To: gcc at gcc dot gnu dot org
- Date: Wed, 11 Mar 2015 17:56:01 +0100
- Subject: Re: Undefined behavior due to 6.5.16.1p3
- Authentication-results: sourceware.org; auth=none
- References: <54FDF3CE dot 6030507 at robbertkrebbers dot nl> <54FF195B dot 5060604 at redhat dot com> <54FF1F7C dot 5050501 at robbertkrebbers dot nl> <54FF2182 dot 1090800 at robbertkrebbers dot nl> <alpine dot DEB dot 2 dot 10 dot 1503102153590 dot 10726 at digraph dot polyomino dot org dot uk> <550042BD dot 8000608 at robbertkrebbers dot nl> <20150311160815 dot GA2967 at ypig dot lip dot ens-lyon dot fr> <20150311161155 dot GM1746 at tucnak dot redhat dot com> <20150311163101 dot GA4384 at ypig dot lip dot ens-lyon dot fr> <20150311163931 dot GN1746 at tucnak dot redhat dot com>
On 2015-03-11 17:39:31 +0100, Jakub Jelinek wrote:
> On Wed, Mar 11, 2015 at 05:31:01PM +0100, Vincent Lefevre wrote:
> > > (in C only one union member can be active at any time,
> > > we as extension allow type punning through unions etc.)
> >
> > I disagree that it is an extension. The standard does not say
> > that "one union member can be active at any time".
>
> That is not a standard wording, but what I meant is
> 6.2.6.1p7 - that when you store some union member other union members take
> unspecified values.
Well, the values are unspecified, but the value can sometimes be
deduced from what is stored.
BTW, the C11 draft I have says:
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.
Note the "that do not correspond to that member but do correspond to
other members".
This means that on a machine where an int takes 4 bytes, if one has:
union { unsigned char a[8]; int b; } u = { .a = { 0 } };
u.b = 1;
Then the last 4 bytes of u.a take unspecified values. But the first
4 bytes depend on the representation of the int 1, and if this
representation is well-specified by the implementation (e.g. no
padding bits...), which can sometimes be deduced, then the first
4 bytes of u.a are known and one can access them via u.a.
--
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)