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


Groups > comp.lang.c > #22990

Re: Function casting - UB?

From Tim Rentsch <txr@alumni.caltech.edu>
Newsgroups comp.lang.c
Subject Re: Function casting - UB?
Date 2012-06-29 11:03 -0700
Organization A noiseless patient Spider
Message-ID <kfnd34i2dya.fsf@x-alumni2.alumni.caltech.edu> (permalink)
References <0c176b1e-39f3-4e65-8a7b-eca16b8955c5@d24g2000yqh.googlegroups.com> <kfnzk7o4pxf.fsf@x-alumni2.alumni.caltech.edu> <jsh0qa$49g$1@news.albasani.net>

Show all headers | View raw


Johannes Bauer <dfnsonfsduifb@gmx.de> writes:

> On 27.06.2012 19:37, Tim Rentsch wrote:
>> The other example you give,
>> assigning to one member of a union and reading from another, is
>> actually defined behavior, _not_ undefined behavior.
>
> I also was under the impression that writing to member x and reading
> member y of a union is UB. Wikipedia says "This is not, however, a safe
> use of unions in general.", which is pretty vague (i.e. it's not clear
> which cases are safe and which are not).
>
> Could you elaborate on why you think this is well-defined?

Besides the particular footnote (which you already mentioned in
your own followup), there is just the normative text pertaining to
types and storage access.  If you read through the two main
sections on types (6.2.5 and 6.2.6), and also the description of
what happens on lvalue-to-value conversion, I think it's pretty
easy to see that the definition is there (although I freely admit
it isn't expressed as directly as one might like).  Basically, the
same passages that explain how ordinary (ie, non-union-member)
access works also explain how access to union members work;  the
only thing that's missing is knowing that the respective memories
overlap, which is stated in 6.2.5.  There is another detail having
to do with effective type rules, but that doesn't contribute to
defining the semantics;  it just needs to be checked to make sure
the effective type rules don't _un_define the semantics (and they
don't, but if you're interested look at 6.5 p6&7).

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


Thread

Function casting - UB? nroberts <roberts.noah@gmail.com> - 2012-06-27 08:50 -0700
  Re: Function casting - UB? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-27 12:49 -0400
    Re: Function casting - UB? Heikki Kallasjoki <fis+usenet@zem.fi> - 2012-06-27 17:54 +0000
      Re: Function casting - UB? "io_x" <a@b.c.invalid> - 2012-06-28 18:58 +0200
  Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-27 10:37 -0700
    Re: Function casting - UB? Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-28 09:23 +0200
      Re: Function casting - UB? Johannes Bauer <dfnsonfsduifb@gmx.de> - 2012-06-28 09:27 +0200
        Re: Function casting - UB? Jens Gustedt <jens.gustedt@loria.fr> - 2012-06-28 12:03 +0200
          Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-29 11:10 -0700
      Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-29 11:03 -0700
  Re: Function casting - UB? Les Cargill <lcargill99@comcast.com> - 2012-06-27 12:58 -0500
  Re: Function casting - UB? Les Cargill <lcargill99@comcast.com> - 2012-06-27 12:59 -0500
    Re: Function casting - UB? James Kuyper <jameskuyper@verizon.net> - 2012-06-27 14:45 -0400
      Re: Function casting - UB? "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-06-27 14:55 -0700
        Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-27 17:44 -0700
          Re: Function casting - UB? Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-07 16:46 -0700
            Re: Function casting - UB? Barry Schwarz <schwarzb@dqel.com> - 2012-07-08 09:19 -0700
              Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-08 11:13 -0700
                Re: Function casting - UB? Barry Schwarz <schwarzb@dqel.com> - 2012-07-08 15:24 -0700
                Re: Function casting - UB? Joshua Maurice <joshuamaurice@gmail.com> - 2012-07-08 18:12 -0700
                Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-10 10:40 -0700
            Re: Function casting - UB? Tim Rentsch <txr@alumni.caltech.edu> - 2012-07-08 11:06 -0700

csiph-web