Strange IL code from simple C# expression
-
Hello, guys I found an interesting topic on MS newsgroup. And I'd like to ask you a simple question. What is the value of "
i
" variable after the code ?int i = 5; i = i++;
??? Provide me, please, with an answer you though/expected just after you looked at the code. And then, any ideas why does C# compiler work so ? (if you will run the code, you will understand what I mean). With best regards, Andrew
Problem is there in your example. Try to use different variable to distingush the difference.
int i=5,j=0; j=i++;
Sreejith Nair [ My Articles ] -
Problem is there in your example. Try to use different variable to distingush the difference.
int i=5,j=0; j=i++;
Sreejith Nair [ My Articles ] -
You wrote : Provide me, any ideas why does C# compiler work so ? I post answer for your queary. And sorry if you twist the issue. Sreejith Nair [ My Articles ]
-
Hello, guys I found an interesting topic on MS newsgroup. And I'd like to ask you a simple question. What is the value of "
i
" variable after the code ?int i = 5; i = i++;
??? Provide me, please, with an answer you though/expected just after you looked at the code. And then, any ideas why does C# compiler work so ? (if you will run the code, you will understand what I mean). With best regards, Andrew
IMO this is a compiler bug. From the IL you can see there is some funniness. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots
-
Hello, guys I found an interesting topic on MS newsgroup. And I'd like to ask you a simple question. What is the value of "
i
" variable after the code ?int i = 5; i = i++;
??? Provide me, please, with an answer you though/expected just after you looked at the code. And then, any ideas why does C# compiler work so ? (if you will run the code, you will understand what I mean). With best regards, Andrew
Andrew Kirillov wrote: int i = 5; i = i++; Easy, 5. The value of
i
is incremented after the expression is evaluated. In this example: Andrew Kirillov wrote: int i = 5; i = ++i; Here, i will be 6. The value ofi
is incremented before the expression is evaluated. This is all defined functionality in the C# language specification. C and C++ do the same thing. 7.5.9 Postfix increment and decrement operators[^] RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome -
Andrew Kirillov wrote: int i = 5; i = i++; Easy, 5. The value of
i
is incremented after the expression is evaluated. In this example: Andrew Kirillov wrote: int i = 5; i = ++i; Here, i will be 6. The value ofi
is incremented before the expression is evaluated. This is all defined functionality in the C# language specification. C and C++ do the same thing. 7.5.9 Postfix increment and decrement operators[^] RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming GnomeDave Kreskowiak wrote: C and C++ do the same thing. No ! I wrote it alredy. I've tested it with VS.NET 2003. C++ (managed and unmanaged) gives 6. With best regards, Andrew
-
Dave Kreskowiak wrote: C and C++ do the same thing. No ! I wrote it alredy. I've tested it with VS.NET 2003. C++ (managed and unmanaged) gives 6. With best regards, Andrew
Andrew Kirillov wrote: No ! I wrote it alredy. I've tested it with VS.NET 2003. C++ (managed and unmanaged) gives 6. Then I'd say the problem is with C++ not with C#. Try this:
int i = 5; if (i++ == 5) { Console::WriteLine("Five"); } else { Console::WriteLine("Six"); }
In C# and C++ you should get "Five" as a result. -
Andrew Kirillov wrote: No ! I wrote it alredy. I've tested it with VS.NET 2003. C++ (managed and unmanaged) gives 6. Then I'd say the problem is with C++ not with C#. Try this:
int i = 5; if (i++ == 5) { Console::WriteLine("Five"); } else { Console::WriteLine("Six"); }
In C# and C++ you should get "Five" as a result.Wjousts wrote: Try this: .... In C# and C++ you should get "Five" as a result. Yes, I know. It will be the same in both languages. I was interested in original code I wrote. With best regards, Andrew
-
Dave Kreskowiak wrote: C and C++ do the same thing. No ! I wrote it alredy. I've tested it with VS.NET 2003. C++ (managed and unmanaged) gives 6. With best regards, Andrew
So it does... It looks like your right. So what's the point of all this? RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Wjousts wrote: Try this: .... In C# and C++ you should get "Five" as a result. Yes, I know. It will be the same in both languages. I was interested in original code I wrote. With best regards, Andrew
-
Actually you are right, it should be 6 (in your original code) in both C++ and C#. Nasty trick question that. So it would appear to be a problem with the C# compiler.
After doing some digging, the evaluation rules are slightly different between the C++ language specifications and the C# specs. It's not a bug as far the specifications describes the process. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
After doing some digging, the evaluation rules are slightly different between the C++ language specifications and the C# specs. It's not a bug as far the specifications describes the process. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
No, I think it is a bug, evaulation rules or not. It's very easy to miss (and I did at first) but the prefix/postfix thing is irrelevant:
int i = 5; i = i++;
versusint i = 5; i = ++i;
Should both end up with i = 6. i++ is the same as i = i + 1 (so is ++i) so really the both code snippets should becomeint i = 5; i = i; i = i + 1;
andint i = 5; i = i + 1; i = i;
It shouldn't matter if you doi = i
first ori = i + 1
first the result should still be six. As somebody else pointed out, it you doj = i++
then look at the value of j then the prefix/postfix part matters, but because you are putting the result back in to i it should be six either way. -
No, I think it is a bug, evaulation rules or not. It's very easy to miss (and I did at first) but the prefix/postfix thing is irrelevant:
int i = 5; i = i++;
versusint i = 5; i = ++i;
Should both end up with i = 6. i++ is the same as i = i + 1 (so is ++i) so really the both code snippets should becomeint i = 5; i = i; i = i + 1;
andint i = 5; i = i + 1; i = i;
It shouldn't matter if you doi = i
first ori = i + 1
first the result should still be six. As somebody else pointed out, it you doj = i++
then look at the value of j then the prefix/postfix part matters, but because you are putting the result back in to i it should be six either way.According the doc's I read in the specifications, the result your seeing is the expected result of the expression. C# in 2002, 2003, and 2005 do the exact same thing. The specifications between versions didn't change. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
According the doc's I read in the specifications, the result your seeing is the expected result of the expression. C# in 2002, 2003, and 2005 do the exact same thing. The specifications between versions didn't change. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
The specifications concerning the behavor of postfix and prefix increment aren't relavent. The result should be 6 either way!
i = i++;
i gets assigned 5 and then i gets incremented. Therefore i = 6.Wjousts wrote: i gets assigned 5 and then i gets incremented. Therefore i = 6. That's not the normal execution order. The real execution order (in C#) is: Read value of i Increment i Assign old value of i to i Why should the "++" wait until after the assignment? It directly binds to "i": AssignmentExpression: Left=i Op=Assign Right=[UnaryExpression: Op=PostIncrement Expr=i] It behaves differently in C++ because C++ has very strange rules for execution order.
-
The specifications concerning the behavor of postfix and prefix increment aren't relavent. The result should be 6 either way!
i = i++;
i gets assigned 5 and then i gets incremented. Therefore i = 6.Wjousts wrote: The specifications concerning the behavor of postfix and prefix increment aren't relavent. WHAT?! :confused: Then why have a language specification at all? They detail the inner workings of the instruction set and execution order. According to the C/C++ language specifications, the value should be 6. Stop applying C/C++ specifications to C#. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Wjousts wrote: The specifications concerning the behavor of postfix and prefix increment aren't relavent. WHAT?! :confused: Then why have a language specification at all? They detail the inner workings of the instruction set and execution order. According to the C/C++ language specifications, the value should be 6. Stop applying C/C++ specifications to C#. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
Dave Kreskowiak wrote: WHAT?! Then why have a language specification at all? They detail the inner workings of the instruction set and execution order. Not relevant to this problem. You are still not getting this. i++ is logically equavalent to i=i+1 correct? If i do i=i+1 I should get the same value in i as I would if I just did i++, correct? So then:
int i = 5; i = i++;
should give the same result as:int i = 5; i = i; i = i + 1;
Correct? Therefore i should equal 6. Show me where the flaw is? Both the assignment and the increment have to happen before the code has finished executing therefore it really doesn't matter which one happens first. Compare these:int i = 5; i++; Console.Writeline(i.ToString());
int i = 5; ++i; Console.Writeline(i.ToString());
What's the output in both cases? Would you agree that the postfix or prefix notation doesn't change the output in this case? -
Dave Kreskowiak wrote: WHAT?! Then why have a language specification at all? They detail the inner workings of the instruction set and execution order. Not relevant to this problem. You are still not getting this. i++ is logically equavalent to i=i+1 correct? If i do i=i+1 I should get the same value in i as I would if I just did i++, correct? So then:
int i = 5; i = i++;
should give the same result as:int i = 5; i = i; i = i + 1;
Correct? Therefore i should equal 6. Show me where the flaw is? Both the assignment and the increment have to happen before the code has finished executing therefore it really doesn't matter which one happens first. Compare these:int i = 5; i++; Console.Writeline(i.ToString());
int i = 5; ++i; Console.Writeline(i.ToString());
What's the output in both cases? Would you agree that the postfix or prefix notation doesn't change the output in this case?Wjousts wrote: i++ is logically equavalent to i=i+1 correct? No, it's not!
i++;
is equivilent toi=i+1
. Your flaw is not understand the execution order of the operators and assignments you're using. This works like you want it to:int i = 5;
i++;
Console.WriteLine(i.ToString());You're not understanding the procedures, order, and times in which each operation takes place. It's already been explained to you in the C# Language Specifications. Wjousts wrote: Would you agree that the postfix or prefix notation doesn't change the output in this case? It's not the operators that are changing the output. It's, again, the execution order and times of the operators in question. What do you think the output of these are?:
int i = 5;
Console.WriteLine((i++).ToString());
int i = 5;
Console.WriteLine((++i).ToString());RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Wjousts wrote: i++ is logically equavalent to i=i+1 correct? No, it's not!
i++;
is equivilent toi=i+1
. Your flaw is not understand the execution order of the operators and assignments you're using. This works like you want it to:int i = 5;
i++;
Console.WriteLine(i.ToString());You're not understanding the procedures, order, and times in which each operation takes place. It's already been explained to you in the C# Language Specifications. Wjousts wrote: Would you agree that the postfix or prefix notation doesn't change the output in this case? It's not the operators that are changing the output. It's, again, the execution order and times of the operators in question. What do you think the output of these are?:
int i = 5;
Console.WriteLine((i++).ToString());
int i = 5;
Console.WriteLine((++i).ToString());RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
Dave Kreskowiak wrote: Wjousts wrote: i++ is logically equavalent to i=i+1 correct? No, it's not! i++; is equivilent to i=i+1. So no, you agree??? You're still missing the point. First, this wasn't my question. i = i++ is redudant and a silly thing to try and do in the first place! The point is that in that statement that the original poster posted the incremement part, the i++ is NEVER EXECUTED. Where in the C# specification does it say that the compiler will randomly ignore increment statements whenever it feel like it? Please so me the exact section that says that. It's not an issue of execution order because ALL THE STATEMENTS ARE SUPPOSED TO BE EXECUTED!
-
Dave Kreskowiak wrote: Wjousts wrote: i++ is logically equavalent to i=i+1 correct? No, it's not! i++; is equivilent to i=i+1. So no, you agree??? You're still missing the point. First, this wasn't my question. i = i++ is redudant and a silly thing to try and do in the first place! The point is that in that statement that the original poster posted the incremement part, the i++ is NEVER EXECUTED. Where in the C# specification does it say that the compiler will randomly ignore increment statements whenever it feel like it? Please so me the exact section that says that. It's not an issue of execution order because ALL THE STATEMENTS ARE SUPPOSED TO BE EXECUTED!
Wjousts wrote: No, it's not! i++; is equivilent to i=i+1. So no, you agree??? Whoops! My typeo.
i++
is equivelent toi=i+1
. But, that not whati=i++;
is saying. Wjousts wrote: It's not an issue of execution order because ALL THE STATEMENTS ARE SUPPOSED TO BE EXECUTED! Yeah, all the statements are supposed to be executed, DUH! The problem is in which order the various PIECES of those statements are executed! Want proof of the execution order of the expression? One more time...int i = 5;
Console.WriteLine((i++).ToString()); ' Result: 5
int i = 5;
Console.WriteLine((++i).ToString()); ' Result: 6I understand perfectly what the code is doing and I know exactly why it's coming up with the results it is. It's your argument that I don't get. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome