473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

c# Post-incrementing problem

I found a problem with C# and post increments. I was going
through some source code in c++ and found someone did a
post increment:

int x=0;
for(int i=0; i<10; i++)
{
x = x++;
}

In c++, you'd get 0,1,...,9 (as expected). This doesn't
work in C#. In C#, you get 0,...,0. It seems the post
increment is ignored in this case. However, the lines:
x=4;
y=x++;
produces y=4, x=5 (as expected)

Should c# be behaving this way or is there a compiler
error? After looking at the assembly code in c++ and c#, it
looks like a bug in c#.
Nov 15 '05 #1
18 1895
On [GMT+0100=CET],
Patrick Wood <pw**********@boeing.com> thought hard and spewed:
I found a problem with C# and post increments. I was going
through some source code in c++ and found someone did a
post increment:

int x=0;
for(int i=0; i<10; i++)
{
x = x++;
}


I tried this code and confirmed it doesn't increment x. Must be a bug.
Nov 15 '05 #2
On [GMT+0100=CET],
Patrick Wood <pw**********@boeing.com> thought hard and spewed:
I found a problem with C# and post increments. I was going
through some source code in c++ and found someone did a
post increment:

int x=0;
for(int i=0; i<10; i++)
{
x = x++;
}


However, post decrement, preincrment, and predecrement work inside the loop.
Only postincrement fails.
Nov 15 '05 #3
Fascinating.

I have to say, the C++ behaviour (which is certainly what happens) is
*not* what I would expect. The C# seems correct.

x++ should increment the value of x but return the old value. Thus on
the first time through the loop, x++ increments x to 1, but returns the
old value of 0. The assignment of this 0 into x should occur *after* the
++, and so x should stay at zero, as it does in C#.

Suppose we declare y as another int.

Replacing the loop body with:

y = x++;
x = y;

gives the behaviour of x staying at 0, because the assigment definitely
occurs after the increment.

Now here's wierd:

x = y = x++;

First time through the loop, y gets 0 and x get 1. I suspect that
there's an optimisation occuring that is making the C++ behaviour wrong.

Regards,

Jasper Kent

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #4
If you use the x = x++ then I get 0's as well...however if you just use x++
all works correctly so you can get whatever you need done.
"Patrick Wood" <pw**********@boeing.com> wrote in message
news:0b****************************@phx.gbl...
I found a problem with C# and post increments. I was going
through some source code in c++ and found someone did a
post increment:

int x=0;
for(int i=0; i<10; i++)
{
x = x++;
}

In c++, you'd get 0,1,...,9 (as expected). This doesn't
work in C#. In C#, you get 0,...,0. It seems the post
increment is ignored in this case. However, the lines:
x=4;
y=x++;
produces y=4, x=5 (as expected)

Should c# be behaving this way or is there a compiler
error? After looking at the assembly code in c++ and c#, it
looks like a bug in c#.

Nov 15 '05 #5
I've already seen similar problems reported on this newsgroup. It boiled
down to a difference between the C# specs and the C++ specs. So, you should
read the language specs in detail, it is probably not a bug.

My 2 cents on this one: the compiler should rather issue at least a warning
on "dangerous" constructs like this one. It should flag code that may have
ambiguous interpretation and it should discourage people from writing it.

Bruno.

"Patrick Wood" <pw**********@boeing.com> a écrit dans le message de
news:0b****************************@phx.gbl...
I found a problem with C# and post increments. I was going
through some source code in c++ and found someone did a
post increment:

int x=0;
for(int i=0; i<10; i++)
{
x = x++;
}

In c++, you'd get 0,1,...,9 (as expected). This doesn't
work in C#. In C#, you get 0,...,0. It seems the post
increment is ignored in this case. However, the lines:
x=4;
y=x++;
produces y=4, x=5 (as expected)

Should c# be behaving this way or is there a compiler
error? After looking at the assembly code in c++ and c#, it
looks like a bug in c#.

Nov 15 '05 #6
I agree with c++/c# producing all 0's with the loop body
you gave because the two statements are not
exactly equivalent.

Take:

x = x++;

Regardless of the order of operations, x will be post
incremented at some time. So, if you post increment before
you do the assignment or after the assignment, you will
still get an incremented x. So, the c++ is right. I don't
see how (regardless of the spec) x could NOT be
incremented in this context.

-----Original Message-----
Fascinating.

I have to say, the C++ behaviour (which is certainly what happens) is*not* what I would expect. The C# seems correct.

x++ should increment the value of x but return the old value. Thus onthe first time through the loop, x++ increments x to 1, but returns theold value of 0. The assignment of this 0 into x should occur *after* the++, and so x should stay at zero, as it does in C#.

Suppose we declare y as another int.

Replacing the loop body with:

y = x++;
x = y;

gives the behaviour of x staying at 0, because the assigment definitelyoccurs after the increment.

Now here's wierd:

x = y = x++;

First time through the loop, y gets 0 and x get 1. I suspect thatthere's an optimisation occuring that is making the C++ behaviour wrong.
Regards,

Jasper Kent

*** Sent via Developersdex http://www.developersdex.com ***Don't just participate in USENET...get rewarded for it!
.

Nov 15 '05 #7
Joe


"Patrick Wood" <pw**********@boeing.com> wrote in message
news:0b****************************@phx.gbl...
I found a problem with C# and post increments. I was going
through some source code in c++ and found someone did a
post increment:

int x=0;
for(int i=0; i<10; i++)
{
x = x++;
}

In c++, you'd get 0,1,...,9 (as expected). This doesn't
work in C#. In C#, you get 0,...,0. It seems the post
increment is ignored in this case. However, the lines:
x=4;
y=x++;
produces y=4, x=5 (as expected)

Should c# be behaving this way or is there a compiler
error? After looking at the assembly code in c++ and c#, it
looks like a bug in c#.


Hi Patrick,

Given:

.locals init ([0] int32 x,
[1] int32 i)

IL_0006: ldloc.0 // push x on the stack - 0
IL_0007: dup // copy top of stack - 0, 0
IL_0008: ldc.i4.1 // push 1 on the stack - 1, 0, 0
IL_0009: add // add 1 and 0 - 1, 0
IL_000a: stloc.0 // pop x from the stack - 0 : x=1
IL_000b: stloc.0 // pop x from the stack - : x=0

At first glance, it seems that IL_000a should have went before IL_0008, but
really it should be looked at in more depth than just the casual look I've
given it.

Instead, it just stores1 into x and then stores 0 into x, which wipes out
the post-increment. Sure looks like a bug.

The second case worked because you assigned the value to y and not x, which
is okay.

Joe
--
http://www.csharp-station.com
Nov 15 '05 #8
It's not a bug. It's basically the same thing you'll see for the old C++
swap trick of a bunch of xor's. The C# spec works differently.
http://groups.google.com/groups?hl=e...gbl%26rnum%3D1
--
Mike Mayer
http://www.mag37.com/csharp/
mi**@mag37.com
"Patrick Wood" <pw**********@boeing.com> wrote in message
news:0b****************************@phx.gbl...
I found a problem with C# and post increments. I was going
through some source code in c++ and found someone did a
post increment:

int x=0;
for(int i=0; i<10; i++)
{
x = x++;
}

In c++, you'd get 0,1,...,9 (as expected). This doesn't
work in C#. In C#, you get 0,...,0. It seems the post
increment is ignored in this case. However, the lines:
x=4;
y=x++;
produces y=4, x=5 (as expected)

Should c# be behaving this way or is there a compiler
error? After looking at the assembly code in c++ and c#, it
looks like a bug in c#.

Nov 15 '05 #9
"Alex" <a.*******@verizon.net> wrote in message
news:u3**************@tk2msftngp13.phx.gbl...
On [GMT+0100=CET],
Patrick Wood <pw**********@boeing.com> thought hard and spewed:

However, post decrement, preincrment, and predecrement work inside the loop. Only postincrement fails.


Not true. Pose decrement works the same as post increment, as I would
expect.

public static void Main ()
{
Console.WriteLine ("Testing x=x++;");
int x = 100;
for(int i=0; i < 5; i++)
{
x = x++;
Console.Write (x + " ");
}
Console.WriteLine (" x = " + x);

Console.WriteLine ("Testing y=y--;");
int y = 100;
for(int i=0; i < 5; i++)
{
y = y--;
Console.Write (y + " ");
}
Console.WriteLine (" y = " + y);

Console.ReadLine();
}

RESULTS

Testing x=x++;
100 100 100 100 100 x = 100
Testing y=y--;
100 100 100 100 100 y = 100
Nov 15 '05 #10
Sure x++ causes x to get incremented at some time, but the postfix
operator returns the *old* value of x (remember the joke that C++ should
be called ++C, because C++ returns the old value?).

Thus it is the old value (0) that is assigned into x, and hence if this
assignment is done after the ++ (as it seems it sould be) then x, having
gone up to 1, returns to 0.

The behaviour of:

x = y = x++;

giving different values of x and y seems to make it pretty obvious
(whatever the actual values) that something is wrong.

Jasper Kent

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #11
Patrick Wood wrote:
I found a problem with C# and post increments. I was going
through some source code in c++ and found someone did a
post increment:

int x=0;
for(int i=0; i<10; i++)
{
x = x++;
}

In c++, you'd get 0,1,...,9 (as expected).


In C++, the behavior of this code is undefined. 'x' is modified twice
between sequence points (once by ++, once by =), so the C++ standard
makes no guarantees about the code's behavior. You can't expect the
code to do anything in particular.

--
David Olsen
qg********@yahoo.com

Nov 15 '05 #12
"Patrick Wood" <pw**********@boeing.com> wrote in message
news:0b****************************@phx.gbl...
I found a problem with C# and post increments. I was going
through some source code in c++ and found someone did a
post increment:

int x=0;
for(int i=0; i<10; i++)
{
x = x++;
}

In c++, you'd get 0,1,...,9 (as expected). This doesn't
work in C#. In C#, you get 0,...,0. It seems the post
increment is ignored in this case. However, the lines:
x=4;
y=x++;
produces y=4, x=5 (as expected)

Should c# be behaving this way or is there a compiler
error? After looking at the assembly code in c++ and c#, it
looks like a bug in c#.


I'll retract my previous thoughts on this because the specs appear to
describe this behavior as correct:

14.5.9 Postfix increment and decrement operators

.. If x is classified as a variable:
x is evaluated to produce the variable.
The value of x is saved.
The selected operator is invoked with the saved value of x as its argument.
The value returned by the operator is stored in the location given by the
evaluation of x.
The saved value of x becomes the result of the operation.

Here's my interpretation of what this says:

using System;

class PostIncrementTest
{
static void Main()
{
int x = 0;

// * x is evaluated to produce the variable.
// * The value of x is saved.
int savedX = x;

// * The selected operator is invoked with
// the saved value of x as its argument.
// * The value returned by the operator
// is stored in the location given by the
// evaluation of x.
x = savedX + 1;

// * The saved value of x becomes the result
// of the operation.
x = savedX;

Console.WriteLine("x = {0}", x);

//
// However, when y is used you get this
//

x = 0;
int y = 0;

// * x is evaluated to produce the variable.
// * The value of x is saved.
savedX = x;

// * The selected operator is invoked with
// the saved value of x as its argument.
// * The value returned by the operator
// is stored in the location given by the
// evaluation of x.
y = savedX + 1;

// * The saved value of x becomes the result
// of the operation.
x = savedX;

Console.WriteLine("x = {0}, y = {1}", x, y);

Console.ReadLine();

}
}

And the output:

x = 0
x = 0, y = 1

To be sure, I'd be interested in seeing if someone from MS could clarify.

Joe
--
http://www.csharp-station.com

Nov 15 '05 #13
Patrick Wood wrote:
I found a problem with C# and post increments. I was going
through some source code in c++ and found someone did a
post increment:

int x=0;
for(int i=0; i<10; i++)
{
x = x++;
}

In c++, you'd get 0,1,...,9 (as expected). This doesn't
work in C#. In C#, you get 0,...,0. It seems the post
increment is ignored in this case. However, the lines:
x=4;
y=x++;
produces y=4, x=5 (as expected)

Should c# be behaving this way or is there a compiler
error? After looking at the assembly code in c++ and c#, it
looks like a bug in c#.


Actually, you are relying on undefined behavior in the C++ sample. Look
at the C Language FAQ, items 3.1 to 3.4:

http://www.eskimo.com/~scs/C-faq/s3.html

My reading of the C# Language Specification (section 7.2.1 Operator
precedence and associativity) indicates that the "x = x++" expression is
well-defined, and will result in x remaining the same after the entire
expression is complete.

--
mikeb

Nov 15 '05 #14
According to the C# language specification, in 14.5.9 Paragraph 5,
line 2, states:

"The result of x++ or x--is the value of x before the operation,
whereas the result of ++x or --x is the value of x after the
operation."

You can check this out yourself:

int x=0;
MessageBox.Show(x++.ToString()); //shows "0"

And the reason "x=x++;" doesn't do anything is because assignment
occurs before increment (hence post-*), and the *value* of x is
increased, but is not stored (as assignment has already occured).

Austin Ehlers
On Wed, 10 Sep 2003 12:48:25 -0700, "Patrick Wood"
<pw**********@boeing.com> wrote:
I found a problem with C# and post increments. I was going
through some source code in c++ and found someone did a
post increment:

int x=0;
for(int i=0; i<10; i++)
{
x = x++;
}

In c++, you'd get 0,1,...,9 (as expected). This doesn't
work in C#. In C#, you get 0,...,0. It seems the post
increment is ignored in this case. However, the lines:
x=4;
y=x++;
produces y=4, x=5 (as expected)

Should c# be behaving this way or is there a compiler
error? After looking at the assembly code in c++ and c#, it
looks like a bug in c#.


Nov 15 '05 #15
Pat
Yes, I agree that for the x++ portion, x will take on the
value of x before the statement (ie. x=0), but my
understanding was that x++ has an implicit assignment. In
other words, I thought x++ is equivalent to x=x+1

Regardless, it seems wrong to me, but may be right
according to the c# spec.

-----Original Message-----
According to the C# language specification, in 14.5.9 Paragraph 5,line 2, states:

"The result of x++ or x--is the value of x before the operation,whereas the result of ++x or --x is the value of x after theoperation."

You can check this out yourself:

int x=0;
MessageBox.Show(x++.ToString()); //shows "0"

And the reason "x=x++;" doesn't do anything is because assignmentoccurs before increment (hence post-*), and the *value* of x isincreased, but is not stored (as assignment has already occured).
Austin Ehlers
On Wed, 10 Sep 2003 12:48:25 -0700, "Patrick Wood"
<pw**********@boeing.com> wrote:
I found a problem with C# and post increments. I was goingthrough some source code in c++ and found someone did a
post increment:

int x=0;
for(int i=0; i<10; i++)
{
x = x++;
}

In c++, you'd get 0,1,...,9 (as expected). This doesn't
work in C#. In C#, you get 0,...,0. It seems the post
increment is ignored in this case. However, the lines:
x=4;
y=x++;
produces y=4, x=5 (as expected)

Should c# be behaving this way or is there a compiler
error? After looking at the assembly code in c++ and c#, itlooks like a bug in c#.


.

Nov 15 '05 #16
Pat <pw**********@boeing.com> wrote:
Yes, I agree that for the x++ portion, x will take on the
value of x before the statement (ie. x=0), but my
understanding was that x++ has an implicit assignment.
x++ has an implicit assignment to x, yes. However, that's effectively
overridden by the later assignment to x.
In other words, I thought x++ is equivalent to x=x+1
Yes, it is - but the value of the expression is the *original* value,
and that's what then gets assigned by the "x=" part of x=x++;
Regardless, it seems wrong to me, but may be right
according to the c# spec.


Basically remember that the increment happens immediately the
expression is evaluated, *not* after the whole statement is evaluated.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #17
"Pat" <pw**********@boeing.com> wrote in message
news:07****************************@phx.gbl...
Yes, I agree that for the x++ portion, x will take on the
value of x before the statement (ie. x=0), but my
understanding was that x++ has an implicit assignment. In
other words, I thought x++ is equivalent to x=x+1

Regardless, it seems wrong to me, but may be right
according to the c# spec.


Look at it this way:

x = x++; is equivalent to x = x.operator++();

where int.operator++() translates to something like

{
int temp = this;
this = this + 1;
return temp;
}

Now the behaviour you see makes sense, doesn't it.
Nov 15 '05 #18
This is the crucial point, and one I was just about to make.

People assume if their C++ compiler does something, then every other C++
compiler should do the same.

I'm not sure why you'd want to do this in C# anyway, it's absolutely
horrible code.
"David Olsen" <qg********@yahoo.com> wrote in message
news:uM****************@TK2MSFTNGP11.phx.gbl...
Patrick Wood wrote:
I found a problem with C# and post increments. I was going
through some source code in c++ and found someone did a
post increment:

int x=0;
for(int i=0; i<10; i++)
{
x = x++;
}

In c++, you'd get 0,1,...,9 (as expected).


In C++, the behavior of this code is undefined. 'x' is modified twice
between sequence points (once by ++, once by =), so the C++ standard
makes no guarantees about the code's behavior. You can't expect the
code to do anything in particular.

--
David Olsen
qg********@yahoo.com

Nov 15 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Spud | last post by:
<?php // pullpage function by Nick bouton http://www.nickbouton.com/. $CustomerID = "IDHERE"; $method = "POST"; $host = "xml.mydata.com"; $usepath = "/xml.asp"; //print all vars in an...
1
by: Alec | last post by:
Hi All, I have recently built a site using PHP and MySQL, and started to implement a basic forum into it. However, the form to post messages doesnt do what I want it to. If there is no user...
15
by: Thomas Scheiderich | last post by:
I am trying to understand Session variables and ran into a question on how they work with data that is passed. I have an HTM file that calls an ASP file and sends the name either by GET or POST....
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
9
by: c676228 | last post by:
Hi, I am new to this discussion forum. I started to post questions on this forum since this Jan. and got many good responses and I am very appreciated to those who are willing to help with their...
6
by: Brybot | last post by:
I am trying to allow HTTP POST file uploads to my web service. Currently I have it working perfectly for a SOAP/XML request reading in a byte using MemoryStream/FileStream but I cannot figure out...
10
by: Peter Michaux | last post by:
Hi, All Ajax libraries I've read use encodeURIComponent() on the name- value pairs extracted from forms before POST ing the result to the server with and xmlhttprequest. I can understand why...
56
by: UKuser | last post by:
Hi, I'm not sure if this can be done as I've searched the web and this forum. I am using an online merchant provider and I must post certain variables to their webforms through a form on my...
3
by: Jag | last post by:
Hi I am facing a strange issue. I have 3 ASP pages in the default website 1. auth.aspx <html> <body>
9
by: CindyH | last post by:
Hi Trying to get this code to work for http xml post. I need the post to be xml (doc.outerxml) sent in single key name as stream. The following is the post code and code for receiving the request....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.