tags:

views:

497

answers:

2

I recently learned about the , operator and the fact that it introduces a sequence point.

I also learned that the following code led to undefined behavior:

i = ++i;

Because i was modified twice between two sequence points.

But what about the following codes ?

i = 0, ++i;
i = (0, ++i);

While I know the rules, I can't get to a conclusion. So is it defined behavior or not ?

edit: Just as @paxdiablo mentions, defined or not, this is really a bad practice which should be avoided. This question is asked solely for educational purposes and better understanding of the "rules".

+23  A: 

Yes. = has higher precedence than ,, so this expression is equivalent to (i = 0), ++i. , is a sequence point, so it's guaranteed that the ++i occurs after the assignment.

I'm not sure whether i = (0, ++i) is defined though. My guess would be no; there's no sequence point between the increment and the assignment.

Oli Charlesworth
I came up to the same intuition but the fact I can't be sure bothers me a lot. Thanks.
ereOn
@ereOn: My intuition is that because the standard doesn't guarantee it, it's undefined.
Oli Charlesworth
Because of the parentheses, the increment will be evaluated before the assignment, so I also think it will be undefined.
codymanix
@codymanix: If it is guaranteed to be evaluated **before** the assignement, doesn't it then become **defined** ?
ereOn
Sequence points are always between evaluations, and sequence the side effects caused by them. There is a sequence point between "0" and "++i". But not between the assignment and the increment, as @Oli pointed out. For examples like `i = (0, ++i, 0)`, I don't think the C++03 Standard is clear about. I think you could make a point for it to be defined, but I will never rely on it: To me it is just undefined aswell.
Johannes Schaub - litb
@Johannes Schaub - litb: Makes sense, indeed.
ereOn
ereOn: a guarantee that an uninitialized value is incremented and then assigned to another variable sounds not very defined. The whole thing about undefined statements in C will always remain a mystery to me. Why is not compiler able to tell you that statement is undefined?
codymanix
@codymanix: Parentheses force operator binding, they don't provide a sequence point.
Oli Charlesworth
I believe that in this case, `i = (0, ++i);` is semantically equivalent to `i = ++i;` which, as has been pointed out earlier, is undefined.
Remoun
@oli: But in this case the parentheses groups the sequence point, which is what matters.
codymanix
@codymanix: It doesn't affect the order of evaluation.
Oli Charlesworth
@codymanix: Just in the same way that the parentheses don't make `i = (++i)` defined.
Oli Charlesworth
+8  A: 
i = 0, ++i;

As the other answer pointed out it is not Undefined Behaviour.

i = (0, ++i);

The behaviour is undefined in this case because there is no sequence point between ++i and assignment to i.

i = (0, ++i, 0)

The behaviour is well defined1 in C++03, IMHO.

1 See extended discussion for a similar expression.

Prasoon Saurav