In the C and C++ languages, the arr[i] = i++;
statement invokes undefined behavior. Why does the statement i = i + 1;
not invoke undefined behavior?
Since this was originally tagged with c and c++ and not any specific version(s), the below answer is a generic answer to the problem. However, please note for c++, C++17
onwards, the behaviour has changed. Please see this answer by Barry to know more.
For the statement
arr[i] = i++;
the value of i
is used in both the operands, RHS(right-hand-side) and LHS(left-hand-side), and in one of the cases, the value is being modified (as a side effect of post ++
) where there's no sequence point in between to determine which value of i
should be considered. You can also check this canonical answer for more on this.
On the other hand, for i = i + 1
, the value of i
is used only in RHS, the computed result is stored in LHS, in other words, there's no ambiguity. We can write the same statement as i++
, which
i
1
i
in a well-defined sequence. Hence, no issues.
Note that this will change in C++17. In C++17, arr[i] = i++
does not invoke undefined behavior. This is due to the following change in [expr.ass]:
In all cases, the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression. The right operand is sequenced before the left operand.
That is, we do i++
then we do arr[i]
then we perform the assignment. The now well-defined ordering is:
auto src = i++;
auto& dst = arr[i];
dst = src;
For C99, we have:
6.5 Expressions
- Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.
In arr[i] = i++
, the value of i
is only modified once. But arr[i]
also reads from i
, and this value is not used to determine the new value of i
. That's why it has undefined behavior.
On the other hand, in i = i + 1
we read i
in order to compute i + 1
, which is used as the new value of i
. Therefore this expression is fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With