A
al.cpwn
why is i=i++ undefined while x=x*x is alright?
why is i=i++ undefined while x=x*x is alright?
peter said:(e-mail address removed) skrev:
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.
CodeCracker said: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.
why is i=i++ undefined while x=x*x is alright?
why is i=i++ undefined while x=x*x is alright?
CodeCracker said: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.
CodeCracker said: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.
There is no way that a serious c++ compiler could generate a code wich
output is 42, I'm using GCC 3.4.4 and the output is 2.
There is no way that a serious c++ compiler could generate a code wich
output is 42,
I'm using GCC 3.4.4 and the output is 2.
Gavin said:42 might be a less *likely* output than 2. But the point is that 42 is
just as *valid* as 2.
There is no way that a serious c++ compiler could generate a code
wich
output is 42, I'm using GCC 3.4.4 and the output is 2.
There is no way that a serious c++ compiler could generate a code wich
output is 42, I'm using GCC 3.4.4 and the output is 2.
AnalogFile said:That's why Deep Thought will not be compiled with version 3.4.4.
When the ever increasing compilation times of GCC will top 7.5
million years, it will.
Gernot said:This is one of the world-wide known jokes - long before the internet.
CodeCracker said: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.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.