Velocity Reviews > C++ > x=x*x vs. i=i++

# x=x*x vs. i=i++

al.cpwn@gmail.com
Guest
Posts: n/a

 03-22-2006
why is i=i++ undefined while x=x*x is alright?

peter koch
Guest
Posts: n/a

 03-22-2006

(E-Mail Removed) skrev:

> why is i=i++ undefined while x=x*x is alright?

Because i=i++ contains to assignments to i. x=x*x only contains one
assignment to x.
It is when the same variable has two assignments without an intervening
sequenceproblem, you get into troubles.

Peter

Mark P
Guest
Posts: n/a

 03-22-2006
peter koch wrote:
> (E-Mail Removed) skrev:
>
>> why is i=i++ undefined while x=x*x is alright?

>
> Because i=i++ contains to assignments to i. x=x*x only contains one
> assignment to x.
> It is when the same variable has two assignments without an intervening
> sequenceproblem, you get into troubles.
>
> Peter
>

Out of curiousity, what about i = ++i? In this case it at least seems
that both assignments do the same thing.

Clark S. Cox III
Guest
Posts: n/a

 03-22-2006
On 2006-03-21 21:04:16 -0500, Mark P
<(E-Mail Removed)> said:

> peter koch wrote:
>> (E-Mail Removed) skrev:
>>
>>> why is i=i++ undefined while x=x*x is alright?

>>
>> Because i=i++ contains to assignments to i. x=x*x only contains one
>> assignment to x.
>> It is when the same variable has two assignments without an intervening
>> sequenceproblem, you get into troubles.
>>
>> Peter
>>

>
> Out of curiousity, what about i = ++i? In this case it at least seems
> that both assignments do the same thing.

It doesn't matter if they "do the same thing". The fact that there are
two of them makes it undefined behavior.

--
Clark S. Cox, III
http://www.velocityreviews.com/forums/(E-Mail Removed)

CodeCracker
Guest
Posts: n/a

 03-22-2006
I think that in case of
i = i++ the problem is when to assign the incremented value of i to i.
Just for clarity if I define i on the left of equality as Li and on the
right of equality as Ri and rewrite the statement then
Li = Ri++;
This line will be executed as follows:
a. Get the value of Ri.
b. Should I assign the output of ++ operator on Ri now?
c. Assign Ri to Li
d. Or should I assign the incremented value of Ri to Ri now?
Since the C++ standard is not clear on this compiler author can
implement it in either of two ways and consequently the difference in
output is possible.
But if I write
Li = ++Ri;
then the intention is very clear. The steps in this case will be
a. Increment Ri
b. Get the value of incremented Ri
c: assign this new value of Ri to Li

I don't think there will be any problem in this second case.

AnalogFile
Guest
Posts: n/a

 03-22-2006
CodeCracker wrote:
> I think that in case of
> i = i++ the problem is when to assign the incremented value of i to i.
> Just for clarity if I define i on the left of equality as Li and on the
> right of equality as Ri and rewrite the statement then
> Li = Ri++;
> This line will be executed as follows:
> a. Get the value of Ri.
> b. Should I assign the output of ++ operator on Ri now?
> c. Assign Ri to Li
> d. Or should I assign the incremented value of Ri to Ri now?
> Since the C++ standard is not clear on this compiler author can
> implement it in either of two ways and consequently the difference in
> output is possible.
> But if I write
> Li = ++Ri;
> then the intention is very clear. The steps in this case will be
> a. Increment Ri
> b. Get the value of incremented Ri
> c: assign this new value of Ri to Li
>
> I don't think there will be any problem in this second case.

What you think is not really relevant.

#include <iostream>
#include <ostream>

int main()
{
int i=1;
i=++i;
std::cout << i;
}

if you compile and execute and it outputs 42 the compiler is not broken
and is behaving according to the standard.

hnaik
Guest
Posts: n/a

 03-22-2006
I think because:

i++ means i += 1
whereas:
x*x is just an expression.

Gernot Frisch
Guest
Posts: n/a

 03-22-2006

> why is i=i++ undefined while x=x*x is alright?

Just don't do it - it's stupid anyway.

Rolf Magnus
Guest
Posts: n/a

 03-22-2006
(E-Mail Removed) wrote:

> why is i=i++ undefined while x=x*x is alright?

i++ writes to i, x*x doesn't write to x.

Gavin Deane
Guest
Posts: n/a

 03-22-2006

CodeCracker wrote:
> I think that in case of
> i = i++ the problem is when to assign the incremented value of i to i.
> Just for clarity if I define i on the left of equality as Li and on the
> right of equality as Ri and rewrite the statement then
> Li = Ri++;
> This line will be executed as follows:
> a. Get the value of Ri.
> b. Should I assign the output of ++ operator on Ri now?
> c. Assign Ri to Li
> d. Or should I assign the incremented value of Ri to Ri now?
> Since the C++ standard is not clear on this compiler author can
> implement it in either of two ways and consequently the difference in
> output is possible.

The C++ standard is perfectly clear. Modifying a value more than once
between sequence points gives undefined behaviour. The compiler could
implement it in either of the two ways you suggest. Or the compiler
could do absolutely anything else it likes.

> But if I write
> Li = ++Ri;
> then the intention is very clear. The steps in this case will be
> a. Increment Ri
> b. Get the value of incremented Ri
> c: assign this new value of Ri to Li

Why do you think those will be the steps, rather than, for eaxmple:

a. Evaluate the expression ++Ri (the value will be Ri + 1)
b. Should I assign the value of the expression ++Ri to Ri now?
c. Assign Ri to Li, but what is the value of Ri?
d. Or should I assign the value of the expression ++Ri to Ri now?

> I don't think there will be any problem in this second case.

There is exactly the same problem with the second case as with the
first. Undefined behaviour.

Gavin Deane