n = n++ is not the same as n = n; n = n + 1;
The post increment in ++ is called a 'side effect'; they do not affect
the value of the expression but they cause a change in the external
environment like memory, input buffer, and output buffer.
C# and Java specification states that all side effects must have taken
place before the value of the expression is assigned to the variable,
hence the meaning of n = n++ in C# and Java is:
temp = n; (evaluate the expression)
n = n + 1; (apply side effect)
n = temp; (assign the value of the expression to the variable)
Whereas C/C++ only requires the side effect to take place before the
next statement, hence you can apply the side effect before assigning
the value of the expression to the variable or after, and it's up to
specific compiler implementation to decide which way to go about it.
In the case of i = i++ + i--; in the compilers we examined above, they
both assign the value of the expression first before applying side
effects. So the translation of i = i++ + i-- is actually:
temp = i + i; ( the value of the expression )
i = temp; ( assigning the value of the expression to the variable )
i = i + 1; ( apply a side effect )
i = i - 1; ( apply the other side effect )
Note that the 2 side effects can be applied in any order since the
language specification does not define the order in which the side
effects must take place.
In any case, Visual Studio disassembly:
int i = 0;
0041172E mov dword ptr [i],0
i = i++ + i--;
00411735 mov eax,dword ptr [i]
00411738 add eax,dword ptr [i]
0041173B mov dword ptr [i],eax
0041173E mov ecx,dword ptr [i]
00411741 sub ecx,1
00411744 mov dword ptr [i],ecx
00411747 mov edx,dword ptr [i]
0041174A add edx,1
0041174D mov dword ptr [i],edx
which is essentially:
temp = i;
temp = temp + i; //effectively temp = i + i
i = temp;
i--; // there's a trick in parsing which makes it easier to parse
backwards
i++;
Whereas GCC 4.3.2 disassembly:
main:
.LFB2:
pushq %rbp
.LCFI0:
movq %rsp, %rbp
.LCFI1:
movl $10, -4(%rbp)
movl -4(%rbp), %eax
addl %eax, %eax
movl %eax, -4(%rbp)
addl $1, -4(%rbp)
subl $1, -4(%rbp)
leave
ret
i = 10;
temp = i;
temp += temp; ( temp = i + i )
i = temp;
i++; // GCC actually apply side effects in different order than Visual
Studio
i--;
When considering ambiguous behaviours, it's best to examine how the
language specification and compiler implementation affect the meaning
of the code.
As for getting different behaviours in C/C++ vs. Java/C#, they are
different languages. So you shouldn't expect them to behave identical
just because the syntax is similar. The story behind it is that C/C++
is old. The language was specified in the 80's and by the time they
found cases of undefined behaviours, there are already plenty of
compiler implementations out in the market that behaves differently
( mostly due to optimization ). So it was difficult to prefer one
interpretation over another.
Whereas by the time Java comes around, these issues are already well-
known. Sun, being the company that controls Java, basically decided
what its behaviour should be and wrote it into the specification.
On Jun 8, 7:48 am, Paul Smith <[email protected]> wrote:
> Well, perhaps it's easier to just say n = n++ is garbage and not code that
> way!
>
> Paul Smith
>
> [email protected]
>
> On Tue, Jun 8, 2010 at 1:44 PM, Eagle <[email protected]> wrote:
> > Reinier,
>
> > In my opinion, '++' is a unary operator. Like all operators,
> > it should return the
> > operated value rather than the old value. For example, if we use unary
> > minus
> > operator as in the following statement-
> > n = -n;
> > what would (and should) happen is first the minus operator would
> > change the
> > sign of its operand, return that value (not the old value) which would
> > come
> > and sit on the RHS of 'equal to' operator, and then the '=' operator
> > would assign
> > this new value to the variable n on LHS.
> > The pre-increment operator has precedence over '=' operator,
> > but the
> > post-increment operator is supposed to have lower precedence than
> > assignment operator.
> > That is why, the following two codes have different values in 'a'-
>
> > // 1
> > int a, b = 10;
> > a = ++b;
>
> > // 2
> > int a, b=10;
> > a = b++;
>
> > In the first, both a and b would become 11; while in the second a
> > would be 10 and b would be
> > 11.
>
> > I am sure, Java must have some compelling reason to create
> > compiler which is working in
> > 'different' way than C/C++ in the original query in this thread (n = n+
> > +). But, I am still not getting
> > it.
>
> > Eagle
>
> > On Jun 7, 11:02 pm, Reinier Zwitserloot <[email protected]> wrote:
> >> Eagle, that analysis doesn't work, because you're confused about
> >> operator precedence. In Java, as in C, the ++ operator has a higher
> >> precedence than assignment. So, the effect of the "++" is applied
> >> BEFORE the effect of the assignment. So, if n starts out as 10, first
> >> the ++ kicks in, making 'n' 11, and then the assignment kicks in,
> >> making n the result of the "n++" expression. Which is clearly 10. C is
> >> inconsistent here because unlike in java, the "++" operator is defined
> >> as a no-op operator that simply returns the thing you ++ed, with a
> >> side-effect applied sometime later to increment said variable. What
> >> actually happens in C is undefined (see excellent sleuthing work by
> >> mtsay) - the result may be 10 or 11 - either answer is acceptable
> >> according to the C spec.
>
> >> In java, the "++" operator is defined as an atomic concept; it
> >> increments the variable and returns its old value. There is no "side
> >> effect to be applied sometime later". This definition matches what
> >> most people think "++" does, in my experience.
>
> >> On Jun 7, 6:26 am, Eagle <[email protected]> wrote:
>
> >> > Hi!
> >> > I find here C/C++ more consistent and Java (umm..) "weird"!
> >> > Because as I understand, n = n++; is equivalent to the following two
> >> > statements-
> >> > n = n; // assignment first, effectively useless
> >> > n = n + 1; // incrementation afterwards
> >> > while n = ++n; is equivalent to the following two statements -
> >> > n = n + 1; // increment first
> >> > n = n; // assign afterwards
> >> > Both these should produce the same result (11 if n=10 before these)
> >> > and this is what happens in C/C++.
> >> > Java, however, produces different results. Surprising!
>
> >> > Eagle
>
> >> > On Jun 6, 8:29 am, Shoubhik <[email protected]> wrote:
>
> >> > > Hello Reinier,
>
> >> > > Thanks a lot. I get your point.
>
> >> > > " n holds value 10 [after int n = 10]
> >> > > n holds value 11 [after n++]
> >> > > n = 10 [after n = (result of n++)] "
>
> >> > > this part of your explanation is extremely helpful. No doubt with the
> >> > > Java experience anymore!
>
> >> > > But isn't the
> >> > > precedence of unary operators Greater than "=" in C also ???
>
> >> > > So I was wondering why the result in C is so wierd :-)
>
> >> > > Hello Vinay,
>
> >> > > "In C++ and Java post increment operator first assigns the value and
> >> > > then
> >> > > increments"
>
> >> > > the confusing part is that the "incrementation" did not take place :-(
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "google-codejam" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group
> > athttp://groups.google.com/group/google-code?hl=en.
--
You received this message because you are subscribed to the Google Groups
"google-codejam" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-code?hl=en.