www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - postincrement behaviour (differences between dmd and gdc)

reply Nicolai Waniek <no.spam thank.you> writes:
Hello again,

I have a huge arguing going on with a friend of mine, which behaviour of
x++ is the right one. This has in fact to do with D, so read on :)

This is our example application:

int x = 5;
x = x++;
x = x++;
print(x);


I argue: the result printed is 7.
He argues: the result printed is 5.

the "++" operator is defined as: "returns the value and then increments
it by 1". so I guess, the execution should be as follows:


int x = 5;
-> x "gains" the value 5.


x = x++;
-> first, (x = x) is executed. afterwards, x is incremented by 1. as x
and x are the same variables, x now has the value 6.

x = x++;
-> the same as above, so x is now 7.


Allright, now let's look what the compilers do:

C/C++ compiled with GCC:	7
java compiled with SUN's:	5
D compiled with DMD:		5
D compiled with GDC:		7

Allright, so please explain this behaviour to me?

Best regards,
Nicolai Waniek
Jan 24 2007
next sibling parent Pragma <ericanderton yahoo.removeme.com> writes:
Nicolai Waniek wrote:
 Hello again,
 
 I have a huge arguing going on with a friend of mine, which behaviour of
 x++ is the right one. This has in fact to do with D, so read on :)
 
 This is our example application:
 
 int x = 5;
 x = x++;
 x = x++;
 print(x);
 
 
 I argue: the result printed is 7.
 He argues: the result printed is 5.
 
 the "++" operator is defined as: "returns the value and then increments
 it by 1". so I guess, the execution should be as follows:
 
 
 int x = 5;
 -> x "gains" the value 5.
 
 
 x = x++;
 -> first, (x = x) is executed. afterwards, x is incremented by 1. as x
 and x are the same variables, x now has the value 6.
 
 x = x++;
 -> the same as above, so x is now 7.
 
 
 Allright, now let's look what the compilers do:
 
 C/C++ compiled with GCC:	7
 java compiled with SUN's:	5
 D compiled with DMD:		5
 D compiled with GDC:		7
 
 Allright, so please explain this behaviour to me?
You forgot this:
 C compiled with GDC:          5
So at least it's consistent D and C for each backend - this looks like a DMC vs GCC problem. :/ If you use a different test, you'll see that DMC/DMD treats the post-increment as truly following the evaluation of the incremented term, within the expression. x = 5; x = x++ + 5; // x is now 10 or x = 5; x = x++ + x; // x is now 11 -- - EricAnderton at yahoo
Jan 24 2007
prev sibling next sibling parent reply Pragma <ericanderton yahoo.removeme.com> writes:
(Edit) fixed compiler comparison

Nicolai Waniek wrote:
 Hello again,
 
 I have a huge arguing going on with a friend of mine, which behaviour of
 x++ is the right one. This has in fact to do with D, so read on :)
 
 This is our example application:
 
 int x = 5;
 x = x++;
 x = x++;
 print(x);
 
 
 I argue: the result printed is 7.
 He argues: the result printed is 5.
 
 the "++" operator is defined as: "returns the value and then increments
 it by 1". so I guess, the execution should be as follows:
 
 
 int x = 5;
 -> x "gains" the value 5.
 
 
 x = x++;
 -> first, (x = x) is executed. afterwards, x is incremented by 1. as x
 and x are the same variables, x now has the value 6.
 
 x = x++;
 -> the same as above, so x is now 7.
 
 
 Allright, now let's look what the compilers do:
 
 C/C++ compiled with GCC:	7
 java compiled with SUN's:	5
 D compiled with DMD:		5
 D compiled with GDC:		7
 
 Allright, so please explain this behaviour to me?
You forgot this:
 C compiled with DMC:          5
So at least it's consistent D and C for each backend - this looks like a DMC vs GCC problem. :/ If you use a different test, you'll see that DMC/DMD treats the post-increment as truly following the evaluation of the incremented term, within the expression. x = 5; x = x++ + 5; // x is now 10 or x = 5; x = x++ + x; // x is now 11 -- - EricAnderton at yahoo
Jan 24 2007
parent Nicolai Waniek <no.spam thank.you> writes:
 
 C compiled with DMC:          5
So at least it's consistent D and C for each backend - this looks like a DMC vs GCC problem. :/
I don't have dmc installed anywhere here, so thank you :)
Jan 24 2007
prev sibling next sibling parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Nicolai Waniek wrote:
 Hello again,
 
 I have a huge arguing going on with a friend of mine, which behaviour of
 x++ is the right one. This has in fact to do with D, so read on :)
 
 This is our example application:
 
 int x = 5;
 x = x++;
 x = x++;
 print(x);
[snip]
 Allright, now let's look what the compilers do:
 
 C/C++ compiled with GCC:	7
 java compiled with SUN's:	5
 D compiled with DMD:		5
 D compiled with GDC:		7
 
 Allright, so please explain this behaviour to me?
For C & C++, IIRC this is one of the traditional examples of undefined behavior (although typically with i instead of x, and only one such statement :p). The technical reason has to do with sequence points, one of the more annoying parts of the respective standards. Basically, the above means the compiler is allowed to emit code that does either, or even something else entirely. So 42 or 2235023 would be an equally acceptable answer as 5, 6 or 7 here. (Typically though, since compilers aren't actively *trying* to screw their users, one of the latter 3 will result) I believe D was designed to be as familiar as possible to C programmers (and therefore, to some degree, those of other C-like languages). IIRC one guideline to achieve this was that code that looks the same should have the same behavior. Therefore, this doesn't surprise me at all. It's in fact exactly what I would expect to happen. If above reasoning is correct, you can't rely on any particular result; it's likely to be different between compilers and can even be different for new versions of the same compiler, or different runs of the same version of a compiler... Conclusion: just don't do stupid **** like above code[1]. [1]: That is, in anything other than such short test programs as the above trying to figure out what the compiler does in such a situation ;)
Jan 24 2007
parent Nicolai Waniek <no.spam thank.you> writes:
 
 Conclusion: just don't do stupid **** like above code[1].
 
 
 
 [1]: That is, in anything other than such short test programs as the
 above trying to figure out what the compiler does in such a situation ;)
Hehe, that's exactly what I told my friend ;)
Jan 24 2007
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
On Wed, 24 Jan 2007 21:31:44 +0100, Nicolai Waniek wrote:

 Hello again,
 
 I have a huge arguing going on with a friend of mine, which behaviour of
 x++ is the right one. This has in fact to do with D, so read on :)
 
 This is our example application:
 
 int x = 5;
 x = x++;
 x = x++;
 print(x);
 
 
 I argue: the result printed is 7.
 He argues: the result printed is 5.
 
 the "++" operator is defined as: "returns the value and then increments
 it by 1". so I guess, the execution should be as follows:
I think that DMD is correct. The result should be 5. Based on the definition above, I think that the example is equivalent to ... int x = 5; int temp; // x = x++; temp = x; // temp is now 5 x = x + 1; // x is now 6 x = temp; // x is now 5 // x = x++; temp = x; // temp is now 5 x = x + 1; // x is now 6 x = temp; // x is now 5 The key phrase is "returns the value and then increments" which I take it to mean that it returns the value of the variable that it had prior to it being incremented. -- Derek Parnell
Jan 24 2007
next sibling parent Nicolai Waniek <no.spam thank.you> writes:
 Based on the definition above, I think that the example is equivalent to
 ...
 
   int x = 5;
   int temp;
 
   // x = x++;
   temp = x;  // temp is now 5
   x = x + 1; // x is now 6
   x = temp;  // x is now 5
 
   // x = x++;
   temp = x;  // temp is now 5
   x = x + 1; // x is now 6
   x = temp;  // x is now 5
 
 The key phrase is "returns the value and then increments" which I take it
 to mean that it returns the value of the variable that it had prior to it
 being incremented.
 
I interpret it that way: "first do everything related to the 'return' part, afterwards increment". therefore, 7 would be the right solution, because in the first step, 5 is the return value and is assigned to x, afterwards and as the final step, x is incremented... ;)
Jan 24 2007
prev sibling parent "Lionello Lunesu" <lionello lunesu.remove.com> writes:
"Derek Parnell" <derek psych.ward> wrote in message 
news:sh2qs4ffr1mg$.1tm119rzt8yyw$.dlg 40tude.net...
 On Wed, 24 Jan 2007 21:31:44 +0100, Nicolai Waniek wrote:

 Hello again,

 I have a huge arguing going on with a friend of mine, which behaviour of
 x++ is the right one. This has in fact to do with D, so read on :)

 This is our example application:

 int x = 5;
 x = x++;
 x = x++;
 print(x);


 I argue: the result printed is 7.
 He argues: the result printed is 5.

 the "++" operator is defined as: "returns the value and then increments
 it by 1". so I guess, the execution should be as follows:
I think that DMD is correct. The result should be 5. Based on the definition above, I think that the example is equivalent to ... int x = 5; int temp; // x = x++; temp = x; // temp is now 5 x = x + 1; // x is now 6 x = temp; // x is now 5 // x = x++; temp = x; // temp is now 5 x = x + 1; // x is now 6 x = temp; // x is now 5 The key phrase is "returns the value and then increments" which I take it to mean that it returns the value of the variable that it had prior to it being incremented.
I agree. Take this code: int f( int i ) { return i; }//nop int x=5; x = f(x++); assert(x==5); x = f(x++); assert(x==5); L.
Jan 24 2007