what is x = x++;

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-
samsumAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

simpsons17371Commented:
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
n_fortynineCommented:
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
n_fortynineCommented:
if you actually want to do it, use prefix notation instead of postfix.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

samsumAuthor Commented:
thanks.
I just want to confirm both c++ and Java is correct by design.

luu-
0
e12voltsdacCommented:
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
n_fortynineCommented:
>> 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
objectsCommented:
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
objectsCommented:
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
n_fortynineCommented:
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
n_fortynineCommented:
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
n_fortynineCommented:
though i wouldn't take back my comment about x = x++; in Java.
0
grg99Commented:
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
objectsCommented:
> you're extracting one of nietod's post answering a similar question.

Yes. Sorry I didn't quote were reference came from.
0
constructorCommented:
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
n_fortynineCommented:
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
n_fortynineCommented:
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
constructorCommented:
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
constructorCommented:
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
samsumAuthor Commented:
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
samsumAuthor Commented:
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
objectsCommented:
> 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
objectsCommented:
> 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
samsumAuthor Commented:
Well, it is not standard.  I guess each has their own ways of implementing the compiler.
0
objectsCommented:
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
samsumAuthor Commented:
Well, it is not standard.  I guess each has their own ways of implementing the compiler.
0
samsumAuthor Commented:
sorry, I refresh the page, it posted two times.
0
nietodCommented:
>> 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
constructorCommented:
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
nietodCommented:
>> 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
constructorCommented:
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
samsumAuthor Commented:
Thanks nietod for the explaination.
0
nietodCommented:
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
objectsCommented:
> Actually, I think it was objects that posted the solution

Such is life :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.