Joona I Palaste wrote:
> Andrey Tarasevich <> scribbled the following:
>> Joona I Palaste wrote:
>>> Someone also mentioned something along the lines of a[a[i]]=i. This I
>>> would consider defined behaviour, because what is being modified is
>>> a[a[i]], and its old value is never used in the expression at all.
>
>> In general case it is possible that a[i] == i, in which case the problem
>> is rather obvious. This example is specifically targeted at this
>> particular situation.
>
> If a[i]==i, then the expression is equivalent to a[i]=i.
That's a pretty strong statement. It would be equivalent if one could be
sure that the original one does not produce UB.
> However, this
> is still very much defined behaviour, as the object being modified is
> a[i], and its old value is not actually used for anything at all in the
> above code.
Huh? What do you mean by "not used"? The expression 'a[a[i]] = i', or
simply 'a[a[i]] = 0' formally violates the requirements if the standard.
In general case it is possible that a[i] == i, which means that this
expression modifies the value of 'a[i]' object (outer 'a[]', assigns 0
to it) and at the same time reads its value (inner 'a[]'). It can't be
said that this act of reading is performed for the sole purpose of
determining the new value of the object, therefore it's UB.
> Note that even though a[i]==i that doesn't mean that
> &(a[i])==&i, i.e. changing one won't affect the other.
It looks like you were looking in wrong direction. The problem is not
with 'i'. The problem is with 'a[i]', which is modified and read at in
the same expression if a[i] == i. The problem still exists, once again, in
a[a[i]] = 0;
--
Best regards,
Andrey Tarasevich
|