Click here to Skip to main content
15,446,613 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
consider this code
MIDL
int j = 1;
j = j++;

amazingly enough it produses 1, 2nd line has no affect.
MSIL looks like
MSIL
---------------------Loc 0  Stack
IL_0000:  nop
IL_0001:  ldc.i4.1            1           Push 1 onto the stack as int32
IL_0002:  stloc.0     1       -           Pop a value from stack into local variable 0
IL_0003:  ldloc.0     1       1           Load local variable 0 onto stack
IL_0004:  dup         1       1,1         Duplicate the value on the top of the stack
IL_0005:  ldc.i4.1    1       1,1,1       Push 1 onto the stack as int32
IL_0006:  add         1       2,1         Add two values, returning a new value
IL_0007:  stloc.0     2       1           Pop a value from stack into local variable 0
IL_0008:  stloc.0     1       -           Pop a value from stack into local variable 0

quite obvious last line is the culprit. It works the same in Java, JavaScript. C/C++ works fine!
Posted
Comments
Nish Nishant 21-Jan-11 14:00pm    
Good question, take a 5!
Rob Philpott 21-Jan-11 14:02pm    
I think I'm missing something here. Are you suggesting in C++, the answer is 2?
Nish Nishant 21-Jan-11 14:31pm    
The VC++ compiler will give 2. It's undefined behavior in the standard, so any other compiler or a future version of the VC++ compiler might return 1 (or some other result).

I would not say it's a bug, C# has different evaluation rules compared to C++.

If you want what you expect, you can do it as:

j = ++j;


And by the way, the behavior for that line of code is undefined in C/C++ and it's up to the individual compiler implementations to decide how to handle this. For more information, google for "C++ sequence points".

BTW the C# behavior is undefined too. A future version of C# may decide to implement this differently (since it's not in the standard). In general it's best to avoid writing code where the same variable is modified multiple times on either side of an assignment operator.
 
Share this answer
 
v3
Comments
#realJSOP 21-Jan-11 14:14pm    
Damn - beat me to it. Proposed as answer
Nish Nishant 21-Jan-11 14:17pm     CRLF
Well it was posted half an hour ago :-) And it's also one of the better and more interesting threads today! Thank you.
#realJSOP 21-Jan-11 14:29pm    
As you might guess, I prefer the questions that are actually worthy of code instead of the ones that scream "I'm too lazy to use google".
Sergey Alexandrovich Kryukov 21-Jan-11 14:38pm    
Funny question :) my 5 for answer.
> C/C++ works fine!

It does not always work - the behavior is not specified, because j = j++ lacks a sequence point[^] between the two assignments.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Jan-11 14:39pm    
Good note - 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900