20

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".

ereOn
  • 53,676
  • 39
  • 161
  • 238
  • 21
    I don't know _why_ people keep asking questions like this. It doesn't matter whether it's undefined, it's crappy code. You should _never_ use it, defined or otherwise. – paxdiablo Oct 18 '10 at 12:12
  • 10
    @paxdiablo: Do you really think I'd use such a useless statement in real code ?! It's only about understanding the rules better. Nothing more. – ereOn Oct 18 '10 at 12:14
  • @ereOn it's good to learn the rules, that's true. But after a while experience usually leads coders to avoid any constructs that are not well-defined and in common use across all platforms. – PP. Oct 18 '10 at 12:23
  • @PP: Fair enough. I probably made it sound like I was going to use it in my own code but actually, I would be one of the first to complain if there was crappy code like this in our codebase. It was just a question that came to me this morning and which I couldn't get rid of. :) – ereOn Oct 18 '10 at 12:26
  • 8
    @paxdiablo: In Maths you never try divide by zero. Half of Analysis is based around the behaviour of just that though, since it helps you understand how numbers work better. It's a good thing to question why you shouldn't do things, and what happens when you do, you learn about a lot more than just that single behaviour. – AaronM Oct 18 '10 at 13:43
  • 1
    @AaronM: Analyzing consequences is one thing, tossing some Is, plusses, and punctuation before a question mark is another. I'd swear I've seen ~15 variations on this topic by now, with very little or no analysis from the OP each time. –  Oct 19 '10 at 07:37

2 Answers2

26

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.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • I came up to the same intuition but the fact I can't be sure bothers me a lot. Thanks. – ereOn Oct 18 '10 at 12:08
  • @ereOn: My intuition is that because the standard doesn't guarantee it, it's undefined. – Oliver Charlesworth Oct 18 '10 at 12:14
  • Because of the parentheses, the increment will be evaluated before the assignment, so I also think it will be undefined. – codymanix Oct 18 '10 at 12:16
  • @codymanix: If it is guaranteed to be evaluated **before** the assignement, doesn't it then become **defined** ? – ereOn Oct 18 '10 at 12:17
  • 4
    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 Oct 18 '10 at 12:18
  • @Johannes Schaub - litb: Makes sense, indeed. – ereOn Oct 18 '10 at 12:21
  • 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 Oct 18 '10 at 12:30
  • 1
    @codymanix: Parentheses force operator binding, they don't provide a sequence point. – Oliver Charlesworth Oct 18 '10 at 12:56
  • 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 Oct 18 '10 at 13:44
  • @oli: But in this case the parentheses groups the sequence point, which is what matters. – codymanix Oct 19 '10 at 12:42
  • 2
    @codymanix: Just in the same way that the parentheses don't make `i = (++i)` defined. – Oliver Charlesworth Oct 19 '10 at 14:28
7
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.

Community
  • 1
  • 1
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345