?
Solved

what is x = x++;

Posted on 2003-03-20
33
Medium Priority
?
1,322 Views
Last Modified: 2010-05-18
void main()
{
     int x=0;
     x=x++;
     std::cout << x;
     
     
}

The output is ONE.
-----
In Java the ouput is ZERO.

Is this by design?

thanks.

luu-
0
Comment
Question by:samsum
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 7
  • +5
33 Comments
 
LVL 1

Expert Comment

by:simpsons17371
ID: 8177481
in C++ x=x++ can be written as x++ and all it does is adds one to the value of x

if u were to do x=x-- or to type it quicker: x-- (either is fine), it would subtract one from the value of x

why it is not the same in java is beond me, i dont use java

Mark
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8177525
In Java
    int x = 0;
    x = x++;
the original val x is saved (x0rig), then x is incremented,
x0rig is assigned to x, so x will always equal original value. even when you write
    y = 0; x = 0;
    y = x++; then y would still be 0, x is 1.
Java is not C++.
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8177534
if you actually want to do it, use prefix notation instead of postfix.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:samsum
ID: 8177549
thanks.
I just want to confirm both c++ and Java is correct by design.

luu-
0
 

Expert Comment

by:e12voltsdac
ID: 8177559
you shouldn't do x=x++, because x++ already adds one to x and stores it to x, even though this is allowed in c++ there is absolutely no point in restoring x twice.

as for java, when you say x=x++ x will get whatever the return value of x++ is. In C++ it will return x+1, but in java it will return 0 because in java x++ will give no return value.
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8177583
>> but in java it will return 0 because in java x++ will give no return value.
i don't think this matters. simply put, it's because the value of the incremented x is not used the assignment, it instead uses the original value of x.
0
 
LVL 92

Expert Comment

by:objects
ID: 8177777
can anyone explain *why* x=x++; results in the value of x to be 1?
I would have expected it to assign to x the value of evaluating the expression x++, which is 0.
0
 
LVL 92

Expert Comment

by:objects
ID: 8177848
Possibly related to the following, ie. the result is compiler dependant.

From the C++ standard 5.4

4 Except where noted, the order of evaluation of operands of  individual
 operators  and subexpressions of individual expressions, and the order
 in which side effects take place, is unspecified.  Between the  previ-
 ous  and  next  sequence  point  a scalar object shall have its stored
 value modified at most once by the evaluation of an expression.   Fur-
 thermore,  the  prior  value  shall  be accessed only to determine the
 value to be stored.  The requirements of this paragraph shall  be  met
 for  each  allowable  ordering of the subexpressions of a full expres-
 sion; otherwise the behavior is undefined.  [Example:
         i = v[i++];      // the behavior is undefined
         i = 7, i++, i++; // `i' becomes 9

         i = ++i + 1;     // the behavior is undefined
         i = i + 1;       // the value of 'i' is incremented
  --end example]

0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8177875
i think there could be a fine line. the best answer i'd come up with is: x = x++ is equivalent to x = x; x++;
same thing would happen (in C++) of course if you write y = x++; you would get y = 0 and x = 1.
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8177899
you're extracting one of nietod's post answering a similar question. based on this rules, x = x++; is ill-formed and its behavior is then undefined. guess i was wrong then.
>> of course if you write y = x++; you would get y = 0 and x = 1.
then this has nothing to do with x = x++; my bad.
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8177906
though i wouldn't take back my comment about x = x++; in Java.
0
 
LVL 22

Expert Comment

by:grg99
ID: 8177916
What's even weirder, it still does the assignment FIRST, then increments x, even when you add parentheses:
     
   ;         x = (x++);

     mov       ecx,[x]
        mov       [x],ecx
     inc       [x]
 

Zowie!

0
 
LVL 92

Expert Comment

by:objects
ID: 8177917
> you're extracting one of nietod's post answering a similar question.

Yes. Sorry I didn't quote were reference came from.
0
 
LVL 2

Expert Comment

by:constructor
ID: 8177935
samsum, I suggest you complain to the creators of the compiler you are using (hopefully, it's M$). According to the post of objects, they seem to be aware of this bug. It is certainly a deviation from the standard in which x=x++; is well-defined and should result in 0. Such simple expressions should NOT be compiler-dependent! To resolve the expression look to operator precedence (Stroustrup). The expression is equal to x=(x++); resulting in 0 and not (x=x)++; resulting in 1.
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8177977
it's still a very fine line. if we're still talking about the x = x++, once you've determined that it's behavior is undefined, adding a pair of parans won't matter much. Otherwise, talking about y = x++, then it's clear that the result of this and that of y = ++x; have to be different. That's why pre and postfix exist.
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8178010
constructor, x = x++; gives 1 on GCC. there is no telling that x = x++ would act like x=(x++) and not (x=x)++; that's why it is called *undefined*. i've come back and read the thread where nietod and some other people had gone head to head over a similar matter, and it is not pleasant. I suggest that you have a look into it.
www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20163322.html 
0
 
LVL 2

Expert Comment

by:constructor
ID: 8178019
I got my dirty hands on a M$ compiler, and it seems that here x=x++; gives different answers depending on what type/class x is. That's undefined for sure!
Thanx for the ref.
0
 
LVL 2

Expert Comment

by:constructor
ID: 8178074
What a mess! Could we get nietod to comment on the behavior of non-scalar types? Or does anybody else know? Who's stolen my standard(s)?!?
0
 

Author Comment

by:samsum
ID: 8178084
Here is what I think.

x=x++;   //do x=x , then x++

x=(x++); //still do x=x , then x++, because postscript execute at the end, even if the parenthesis.

(x=x)++; // do x=x, then x++
 
0
 

Author Comment

by:samsum
ID: 8178131
The java side has its own stands on explaining why x=x++ is ZERO, which it still not every convincing, because this should be a standard.  Both Java and C++ should behave the same.
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20557983.html
0
 
LVL 92

Expert Comment

by:objects
ID: 8178137
> x=x++;   //do x=x , then x++
> x=(x++); //still do x=x , then x++, because postscript execute at the end, even if the parenthesis.
> (x=x)++; // do x=x, then x++

++ has a higher precendence than = so I'd disagree with the analysis of all 3 comments above.
But it's not an issue of precedence.
0
 
LVL 92

Expert Comment

by:objects
ID: 8178147
> which it still not every convincing

You accepted the other answer so I thought we'd explained it to you. WHich part are you not convinced about?

> Both Java and C++ should behave the same.

Why?

0
 

Author Comment

by:samsum
ID: 8178176
Well, it is not standard.  I guess each has their own ways of implementing the compiler.
0
 
LVL 92

Expert Comment

by:objects
ID: 8178217
Java is standard (as I understand it).

The C++ standard (as I understand the above posted para) states the result of this statement is undefined. Which I guess implies if you want portable code then you shouldn't use it (and I can't see a reason why you would want to use it).

0
 

Author Comment

by:samsum
ID: 8178219
Well, it is not standard.  I guess each has their own ways of implementing the compiler.
0
 

Author Comment

by:samsum
ID: 8178230
sorry, I refresh the page, it posted two times.
0
 
LVL 22

Expert Comment

by:nietod
ID: 8178288
>> Could we get nietod to comment on the behavior of non-scalar types?
Objects found the relevant passage.  The behavior is undefined.  Its undefined for both both scalar types and user-defined classes.
0
 
LVL 2

Expert Comment

by:constructor
ID: 8180015
Thanx for the response, nietod!
This has been quite educational. As my first post in this discussion clearly showed, I did not understand the correct answer when I read it. But I am now a step closer to enlightenment.
One question remains: WHY haven't people behind the standard tried to resolve such expressions by additional rules? Is it logically impossible? To give the compiler-people free hands? Or not to impose additional (temporal/spatial) overhead, that is, optimization?
0
 
LVL 22

Accepted Solution

by:
nietod earned 80 total points
ID: 8180193
>> WHY haven't people behind the standard tried to resolve
>> such expressions by additional rules?
Impossing this rule may make it much harder for the compiler to optimize the code on some platforms.  And this is a case where programmers are concerned about efficiency.  the increment operators are supposed to be the faster way to increment something, preferably more efficient than x = X + 1.   C/C++ does its best to insure that arbitrary rules are not enforced that would reduce performance on some platforms.   For example, it doesn't require that X + Y + Z for built-in types be performed as (X + Y) + Z, it could be performed as X + (Y + Z)  (With some other reestrictions).  Again to give the compiler the opportunity to optimize.

in the case of increment and decrement, a value, the one being changed needs, to be accessed twice and thus is a good candidate for optimizatioN as the vlaue can be stored in a register.. Like

MOV AX,X ; Get X
INC  AX   ; X++
MOV X,AX ; Store X

This is approximately 3 to 5 clock cycles.  But if you force a sequence point after an increment, the compiler must write the register value back to memmory after the increment, and before the assignment.  Thus you get

MOV AX,X   ; Get X.
INC AX    ; X++
MOV X,AX   ; Set X  at sequence point.
MOV AX,X   ; Get X
MOV X,AX   ; Store X.

This is 5 to 8 clock cycles, and more code.   The last two instructions are obviously unneeded, the optimizer might figure that out.   But this is also a very simple case. Things like

SomeFunction(X,X++,-XX)

are much more complex.


0
 
LVL 2

Expert Comment

by:constructor
ID: 8181615
Thanx again nietod! For your effort, I've put an amount of points comparable to the current post out there for you to collect. My questions should probably have been asked in a separate post anyway.

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20558805.html

0
 

Author Comment

by:samsum
ID: 8182335
Thanks nietod for the explaination.
0
 
LVL 22

Expert Comment

by:nietod
ID: 8182554
Actually, I think it was objects that posted the solution.  at least he/she posted the clause from the C++ standard that identifies the problem.
0
 
LVL 92

Expert Comment

by:objects
ID: 8184323
> Actually, I think it was objects that posted the solution

Such is life :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question