Re: Is this UB?




Hi Richard

Both you and Flash have kindly provided explanations, and the penny
has dropped for at one reader as a result, but I'm afraid I still
don't see it...

On 10 Sep, 22:33, Richard Heathfield <r...@xxxxxxxxxxxxxxx> wrote:
In
<c4d67d87-6cd4-4513-a38e-e2f289b97...@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,

Paul N wrote:
Richard

I was puzzled the first time I read the second sentence of the
Standard quoted below, and I'm afraid I'm still confused. It seems
to make some normal-looking statements, such as the one below, into
UB. Presumably I'm reading it wrong. Could you go over it one more
time, please?

Context: *p++ = var;

Okay, let's do a frinstance. var is 6, okay? (Naturally.)

I'm reluctant to describe p with a number, so let's start off with:

int zog[] = { 0xA, 0xB, 0xC, 0xD };

int *p = zog;

So p points to zog[0], i.e. the element with value currently 0xA.

Precedence and associativity give us this AST:

       =
      / \
     *   var
    /
   ++ (post)
  /
 p

We start off (okay, take all when-it-happens phrases with a pinch of
salt, because we might *not* start off...) by accessing p. We do this
so that we can perform the ++ operation, which will, before the next
sequence point, increment p so that it points to zog[1], the element
with value 0xB. We perform the ++ operation, which uses p's value
(only to determine the value to be stored in p),

Promise? You're not going to use this value for any other purpose?

and yields a pointer
value. This pointer value happens to be the same value that p had at
the previous sequence point,

This still seems to me as if either you are reading it twice, or you
are at least using the value you have read for more than one purpose.
It is not in any way a coincidence that the two values are the same.

but it is NOT p ITSELF. It's just a
pointer value. If you like, it's &zog[0].

I appreciate that it is not necessarily the value p has *now*, as p
can be considered to be in a state of flux, as you're about to point
out. But it's the value that p had at the last sequence point. Which
to my mind means that you have read the value of p since that sequence
point to get the value that you are now using. How else can you get
the value?

So... it is this value, not the current value of p, that is yielded by
++. It is necessary to distinguish between them because it is not
clear at this stage what p's value actually is. It might be &zog[0]
or it might be &zog[1], and only the optimiser knows which!

After that, it's plain sailing - the value yielded by ++ (which,
remember, is a different concept to the value of p, even if it
happens to be the same number right now, which it may or may not be)
becomes the operand of *, and that gives us the object zog[0], to
which we assign the value var, and we're done.
.



Relevant Pages

  • Re: Wildly OT: Holy SH!@
    ... And glad you are okay. ... able to smile about it now. ... and a police investigation and eventually there is going to be a trial. ... and I'm not afraid to use 'em. ...
    (rec.music.makers.guitar.acoustic)
  • Re: nVidia TwinView - How to have 1280x1024 Monitor and 800x600 TV?
    ... Sybren Stuvel wrote: ... >>was able to scroll around the TV. ... > I think that's all you can get, I'm afraid. ... Okay. ...
    (alt.os.linux)
  • Re: Saving the children from gay penguins
    ... doesn't mean I'm 'afraid' of it. ... they are not 'afraid' of queers, they disapprove of that lifestyle. ... What makes you so sure that these things aren't normal or okay? ... 04 FXD Stage 1 ...
    (rec.motorcycles)
  • Re: Generating a Poisson Random Variable
    ... I'm afraid I'm not a professional Java programmer, nor a mathematician, but I'd like to be able to generate a set of integers with a Poisson distribution around a starting value. ... sequence of random numbers in the program. ...
    (comp.lang.java.help)
  • Re: HTML Form posting & response parsing
    ... That's what I was afraid of... ... successful workflow, and program a sequence of web requests that contain ...
    (microsoft.public.dotnet.general)

Loading