8

Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)

What is the difference between i = ++i; and ++i; where i is an integer with value 10?

According to me both do the same job of incrementing i i.e after completion of both the expressions i =11.

1

6 Answers 6

10

i = ++i; invokes Undefined Behaviour whereas ++i; does not.

C++03 [Section 5/4] says Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression.

In i = ++i i is being modified twice[pre-increment and assignment] without any intervening sequence point so the behaviour is Undefined in C as well as in C++.

However i = ++i is well defined in C++0x :)

Sign up to request clarification or add additional context in comments.

41 Comments

Predence has nothing to do with the order of evaluation. i = ++i invokes UB because i is being modified more than once between two sequence points.
@DumbCoder: no, it is not a "suprise" if the standard says that something is undefined behavior, and the compiler does something that you consider sensible. Undefined behavior means that the compiler can do anything. Even if it does what you want, it is still strictly following the standard.
@DumbCoder : Why would someone possibly test and predict the behaviour which has already been mandated by the Standard as Undefined. That certainly means you don't understand what Undefined Behaviour means, do you?
@Dumbcoder : I have read and understood what @jalf and @steve said in their comments but being a DumbCoder you just don't seem/want to understand what I have said.
@DumbCoder: "why the compiler was doing the behaviour it was?" is a pointless question if we're talking about UB.
|
7

Writing i = ++i; writes to variable i twice (one for the increment, one for the assignment) without a sequence point between the two. This, according to the C language standard causes undefined behavior.

This means the compiler is free to implement i = ++i as identical to i = i + 1, as i = i + 2 (this actually makes sense in certain pipeline- and cache-related circumstances), or as format C:\ (silly, but technically allowed by the standard).

2 Comments

+1 for mentioning sequence points, something that is very useful to be aware of!
+1 for giving a link to description of sequence points.
3

i = ++i will often, but not necessarily, give the result of

i = i;

i +1;

which gives i = 10

As pointed out by the comments, this is undefined behaviour and should never be relied on

while ++i will ALWAYS give

i = i+1;

which gives i = 11;

And is therefore the correct way of doing it

4 Comments

If you downvote me, then atleast have the courtesy of telling what was wrong with my post
I didn't downvote you, but it might be because your answer is imprecise, saying that it will "often, but not necessarily" give a certain result. It is officially undefined behavior, and trying to guess the odds of which outcome you're going to get might just give a reader the wrong idea, that it is somehow possible to rely on the result.
@jalf - I can't see anything wrong with kskjon's answer. It's not maximally precise but it's totally accurate, and it answers the OP's question. kskjon makes it clear that the i=10 result will "not necessarily" happen. I don't see how that can give a reader an inaccurate idea that it is somehow possible to rely on that result.
@LarsH: Like I said, I didn't downvote it, and you're right, there's nothing factually wrong with his answer. But it does give the impression that you can "usually" rely on a specific answer. Or specifically, by not mentioning that the behavior is actually entirely undefined, it might lead the reader to believe that the behavior is somewhat constrained, that you can still rely on it to do something sane, or that it might behave in one of a set number of ways. I'd be happy if he edited it to include a note that the former is undefined. And if I had to guess, that's why it was downvoted
2

If i is of scalar type, then i = ++i is UB, and ++i is equivalent to i+=1. if i is of class type and there's an operator++ overloaded for that class then i = ++i is equivalent to i.operator=(operator++(i)), which is NOT UB, and ++i just executes the ++ operator, with whichever semantics you put in it.

Comments

1

The result for the first one is undefined.

2 Comments

If and only if ++ is the biult-in ++operator. If it is overloded, then it is NOT undefined behavior
@Armen: that's what I was going to say on Prasoon's answer, then I noticed that the questioner said i was an "integer". I think it's reasonable to assume that this means an integer type, hence UB.
1

These expressions are related to sequence points and, the most importantly, the first one results in undefined behavior.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.