Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #14463

Re: Repeated types in union

From Eric Sosman <esosman@ieee-dot-org.invalid>
Newsgroups comp.lang.c
Subject Re: Repeated types in union
Date 2011-12-10 18:14 -0500
Organization A noiseless patient Spider
Message-ID <jc0p44$dr1$1@dont-email.me> (permalink)
References <jc0l4g$cvr$1@speranza.aioe.org>

Show all headers | View raw


On 12/10/2011 5:06 PM, Edward Rutherford wrote:
> Hello :
>
> Is the following code an undefined behavior?
>
>
>   union {
>          int a;
>          int b;
>   } u;
>   u.a = 3;
>   printf("%d\n", u.b);

     (I rush in where angels fear to tread...)

     First, there's no problem with the issue mentioned in your
subject line: It's perfectly all right to have several union members
with distinct names but the same type.  If that were not so, even
something as simple as `union { int i; time_t t; } u;' could be in
trouble.  See also 6.2.5p20, which says that union members have
"possibly distinct" types.

     The "write one member, read another" question has been discussed
more than once, and my impression of the debates is that there have
been two camps: Not "It's legal" and "It's illegal," but "It's legal"
and "You'll probably get away with it, but it might not be squeaky-
clean, and my head hurts can we talk about something else, please?"
(I'm in the latter camp.)

     It's clear (from 6.2.6.1) that writing `u.a' deposits bytes that
represent `3', and that `u.b' thereby receives the same bytes.  No
argument there: The storage allocated to `u.b' holds a representation
of `3'.

     The part that makes my head ache is figuring out whether the
compiler is required to "notice" that storing to `u.a' affects the
value of `u.b'.  If the compiler has already loaded `u.b' into a
register, say, is it required to re-fetch because `u.a' was changed?
Is the compiler allowed to consider `u.b' uninitialized because it
has never been stored to, despite the store to `u.a'?

     To those in the "It's legal" camp, I offer a few puzzling and
possibly disturbing points:

     - The footnote to 6.2.5p21 points out that "an object with union
       type can only contain one member at a time" -- meaning that if
       `u' contains `u.a', it does not contain `u.b'.  Footnotes, of
       course, are suggestive but non-normative.

     - The footnote to 6.5.2.3p3 supports the "It's legal" camp by
       describing the mechanism of type punning.  Footnotes, of course,
       are suggestive but non-normative.

     - 6.5.2.3p5 gives a "special guarantee" for union members that
       are structs, but does not extend a similar guarantee for other
       member types.

     - 6.7.2.1p14 has the normative language for the first footnote
       mentioned above: "The value of at most one of the members can be
       stored in a union object at any time."  Your `u' can hold `u.a'
       or `u.b', but not both at once.

     Those are the citations I can find (if I've missed any I'm sure
others will point them out).  Their cumulative impression on me is
that the matter is not settled beyond doubt, but the aforementioned
angels may see things differently.

     As a practical matter, it's not all that important what I think
or what the angels think, but what the providers of your compilers
think.  If a compiler does something unfortunate with your code you
will find yourself retracing this same argument with implementors
who are trying to stamp NOT A BUG on your complaint.  If the angels
weigh in on your side, the implementors of the offending compiler
may eventually accede and agree to ship a fix -- "In a forthcoming
release," oh joy, oh joy.  I think you might choose better battles:
Fight over things you Really Really Need and are Really Solid Bugs,
and don't waste troops trying to subjugate the unpopulated hinterland.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Repeated types in union Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2011-12-10 22:06 +0000
  Re: Repeated types in union Jens Gustedt <jens.gustedt@loria.fr> - 2011-12-11 00:11 +0100
  Re: Repeated types in union Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-10 18:14 -0500
    Re: Repeated types in union Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> - 2011-12-12 20:09 +0000
      Re: Repeated types in union Jens Gustedt <jens.gustedt@loria.fr> - 2011-12-12 23:45 +0100
      Re: Repeated types in union Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-12 19:48 -0500
        Re: Repeated types in union "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2011-12-13 15:17 -0800
          Re: Repeated types in union Keith Thompson <kst-u@mib.org> - 2011-12-13 15:29 -0800
          Re: Repeated types in union James Kuyper <jameskuyper@verizon.net> - 2011-12-13 18:30 -0500
          Re: Repeated types in union Noob <root@127.0.0.1> - 2011-12-14 17:40 +0100
          Re: Repeated types in union Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 13:28 -0800
        Re: Repeated types in union Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 15:04 -0800
    Re: Repeated types in union ralph <nt_consulting64@yahoo.net> - 2011-12-12 14:50 -0600
    Re: Repeated types in union Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 14:53 -0800
  Re: Repeated types in union Barry Schwarz <schwarzb@dqel.com> - 2011-12-10 22:04 -0800
    Re: Repeated types in union "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2011-12-12 09:49 -0800
      Re: Repeated types in union Jens Gustedt <jens.gustedt@loria.fr> - 2011-12-12 20:09 +0100
        Re: Repeated types in union "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2011-12-13 15:10 -0800
          Re: Repeated types in union Jens Gustedt <jens.gustedt@loria.fr> - 2011-12-14 00:24 +0100
          Re: Repeated types in union Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 13:46 -0800
      Re: Repeated types in union Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 13:42 -0800
    Re: Repeated types in union Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 13:36 -0800

csiph-web