Isn't a[i++] = 1 (one) where, computation of the increment to be unsequenced relative to the indexing of the array, leading to violation of S6.5.2
Solution 1:
in that case this side-effect is unsequenced to the value computation using the same scaler object.
The scalar object involved in i++
is i
. The side effect of updating i
is not unsequenced relative to the computation of the value of i++
because C 2018 6.5.2.4 (which specifies behavior of postfix increment and decrement operators) paragraph 2 says:
… The value computation of the result is sequenced before the side effect of updating the stored value of the operand…
C 2011 has the same wording. (C 2018 contains only technical corrections and clarifications to C 2011.)
Even from the C99 S6.5(p2)
Furthermore, the prior value shall be read only to determine the value to be stored.
A rule in the C 1999 standards has no application to the 2011 or 2018 standards; it must be interpreted separately. Between 1999 and 2011, the standard moved from solitary sequence points to finer rules about sequencing relationships.
In i++
, the prior value is read to determine what the new value of i
should be, so it conforms to that rule.
The rule was an attempt to say that any reads of a scalar object had to be in the prerequisite chain of an writes of the object. For example, in i = 3*i + i*i
, all three reads of i
are necessary to compute the value to be written to i
, so they are necessarily performed before the write. But in i = ++i + i;
, the read of i
for that last term is not a prerequisite for writing to i
for the ++i
, so it is not necessarily performed before the write. Thus, it would not conform to the rule.
… I am confused that isn't fetching the operation of the array indexing unsequenced relative to the
i++
increment operation?
The read of the array element is unsequenced relative to the update of i
, and that is okay because there is no rule that requires it to be sequenced. C 2018 6.5 2 says, emphasis added:
If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.
The array element is a different scalar object from i
, so we do not care that there is no sequencing between the read of the array element and the update to i
.