Re: Is this UB?
- From: Paul N <gw7rib@xxxxxxx>
- Date: Fri, 11 Sep 2009 10:43:11 -0700 (PDT)
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.
- Follow-Ups:
- Re: Is this UB?
- From: Old Wolf
- Re: Is this UB?
- From: Richard Heathfield
- Re: Is this UB?
- From: Willem
- Re: Is this UB?
- References:
- Is this UB?
- From: Dominik Zaczkowski
- Re: Is this UB?
- From: Richard Heathfield
- Re: Is this UB?
- From: Paul N
- Re: Is this UB?
- From: Richard Heathfield
- Is this UB?
- Prev by Date: Re: Is this UB?
- Next by Date: Re: J'accuse
- Previous by thread: Re: Is this UB?
- Next by thread: Re: Is this UB?
- Index(es):
Relevant Pages
|
Loading