Thread: Undefined behavior

1. Undefined behavior

Hi,

Why does the following code exhibit undefined behavior?
Code:
```int i = 1;
i += ++i;```

Is it because of the following rule?
Between consecutive "sequence points" an object's value can be modified only once by an expression.

I think that it shouldn't be undefined, because:
1) Since ++ has higher precedence than +=, the increment operator is first evaluated. So ++i increments i by one and returns the new value of i, that is 2.
2) Then += is evaluated. The left operand is an object (having number 2) and the right operand is 2. So the sum, of the content of the object and two, is finally stored in the object (that is i).

It's however true that the object's value is modified more than once between the two consecutive sequence points.

Thanks for help...

2. Originally Posted by atran
I think that it shouldn't be undefined, because:
1) Since ++ has higher precedence than +=, the increment operator is first evaluated. So ++i increments i by one and returns the new value of i, that is 2.
2) Then += is evaluated. The left operand is an object (having number 2) and the right operand is 2. So the sum, of the content of the object and two, is finally stored in the object (that is i).
Precedence determines grouping, not order of evaluation. Another plausible interpretation is that on the left hand side of the +=, the i is evaluated in order to determine the result of the += expression, then ++i is evaluated. Therefore, the final value of i should obviously be 3, not 4.

That said, this alternative order of evaluation does not matter because...

Originally Posted by atran
It's however true that the object's value is modified more than once between the two consecutive sequence points.
... the behaviour is undefined.

3. Originally Posted by laserlight
Precedence determines grouping, not order of evaluation.
Thanks.

Is i=++i defined? If so, why?

Also, does an assignment expression return the value assigned to the left operand, or does it instead return the value of the left operand after the actual assignment?

4. Originally Posted by atran
Is i=++i defined? If so, why?
It results in undefined behaviour for the same reason as in post #1.

Originally Posted by atran
Also, does an assignment expression return the value assigned to the left operand, or does it instead return the value of the left operand after the actual assignment?
Refer to the C standard:
Originally Posted by C11 Clause 6.5.16 Paragraph 3
An assignment operator stores a value in the object designated by the left operand. An assignment expression has the value of the left operand after the assignment, but is not an lvalue. The type of an assignment expression is the type the left operand would have after lvalue conversion. The side effect of updating the stored value of the left operand is sequenced after the value computations of the left and right operands. The evaluations of the operands are unsequenced.
Incidentally, speaking of C11, the rule that you mentioned in post #1 has been updated to:
Originally Posted by C11 Clause 6.5 Paragraph 2
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. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.

Popular pages Recent additions