Sequence points and partial order

The C standard says this about assignment operators (C90 6.3.16 or C99 6.5.16 Assignment operators):

The side effect of updating the stored value of the left operand shall occur between the previous and the next sequence point.

It seems to me that in the statement:

i=(i,i++,i)+1;

the sequence point ‘previous’ to the assignment operator would be the second comma operator and the ‘next’ sequence point would be the end of the expression. So I’d say that the expression doesn’t invoke undefined behavior.

However, this expression:

*(some_ptr + i) = (i,i++,i)+1;

would have undefined behavior because the order of evaluation of the 2 operands of the assignment operator is undefined, and in this case instead of the problem being when the assignment operator’s side effect takes place, the problem is you don’t know whether the value of i used in the left handle operand will be evaluated before or after the right hand side. This order of evaluation problem doesn’t occur in the first example because in that expression the value of i isn’t actually used in the left-hand side – all that the assignment operator is interested in is the “lvalue-ness” of i.

But I also think that all this is sketchy enough (and my understanding of the nuances involved are sketchy enough) that I wouldn’t be surprised if someone can convince me otherwise (on either count).

Leave a Comment