473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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

Mar 22 '06 #1
19 2215

al.c...@gmail.com 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

Mar 22 '06 #2
peter koch wrote:
al.c...@gmail.com 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.
Mar 22 '06 #3
On 2006-03-21 21:04:16 -0500, Mark P
<us****@fall2005REMOVE.fastmailCAPS.fm> said:
peter koch wrote:
al.c...@gmail.com 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
cl*******@gmail.com

Mar 22 '06 #4
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.

Mar 22 '06 #5
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.

Mar 22 '06 #6
I think because:

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

Mar 22 '06 #7
why is i=i++ undefined while x=x*x is alright?


Just don't do it - it's stupid anyway.
Mar 22 '06 #8
al*****@gmail.com wrote:
why is i=i++ undefined while x=x*x is alright?


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

Mar 22 '06 #9

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

Mar 22 '06 #10
CodeCracker wrote:
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 quite clear: the behavior of that code is undefined.
That means that the standard does not require any particular behavior
from that code. It's certainly reasonable for a compiler to do what you
suggest, but unless the compiler's documentation tells you that that's
what it does or you've tested it, you're just speculating.

--

Pete Becker
Roundhouse Consulting, Ltd.
Mar 22 '06 #11
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.

Mar 22 '06 #12

se************@gmail.com wrote:
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.


I think you missed the point.

Given this program

#include <iostream>
#include <ostream>

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

42 might be a less *likely* output than 2. But the point is that 42 is
just as *valid* as 2. Any output is as valid as any other. It is
unfortunate that sometimes when you invoke undefined behaviour, you see
exactly the effect you were expecting.

Gavin Deane

Mar 22 '06 #13
Gavin Deane wrote:

42 might be a less *likely* output than 2. But the point is that 42 is
just as *valid* as 2.


It's valid according to the language definition. In fact, a compiler
will produce one of the two results that have been mentioned. The reason
the standard doesn't require one or the other is that this provision
applies to any code that modifies the same value more than once between
sequence points, so it's hard to write an exact specification of the
possible results. Aside from whether it is well-defined, either in
theory or in practice, though, this code is nonsense. Even if it were
well defined it has no reason to exist.

--

Pete Becker
Roundhouse Consulting, Ltd.
Mar 22 '06 #14
In message <11**********************@v46g2000cwv.googlegroups .com>,
"se************@gmail.com" <se************@gmail.com> writes
There is no way that a serious c++ compiler could generate a code wich
output is 42,
A truly serious C++ compiler would refuse to compile such abominations
in the first place.
I'm using GCC 3.4.4 and the output is 2.


So what? According to the C++ standard it's at liberty to produce any
effects it likes.

--
Richard Herring
Mar 22 '06 #15
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.


LOL! If I were to write one, it would!
Mar 22 '06 #16
se************@gmail.com wrote:
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.


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.
Mar 22 '06 #17

"AnalogFile" <ne**@NOSPAMkuva.itREALLYNO> schrieb im Newsbeitrag
news:gY******************@tornado.fastwebnet.it...
se************@gmail.com wrote:
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.


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.


This is one of the world-wide known jokes - long before the internet.
Mar 22 '06 #18
Gernot Frisch wrote:
"AnalogFile" <ne**@NOSPAMkuva.itREALLYNO> schrieb im Newsbeitrag
news:gY******************@tornado.fastwebnet.it...
se************@gmail.com wrote:
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.

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.


This is one of the world-wide known jokes - long before the internet.


Did not mean to be original.
But suspect serghei didn't actually recognize the philosophical
implications of my example (I know you did, from your earlier reply).
I just wanted to give him more keywords to google for.

oh, wait!
You actually mean GCC was faster in pre internet? ;-)
Mar 22 '06 #19

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

Not quite. Here are the semantics for the pre- and postincrement
operators as applied to scalar types:

i++: evaluate to i, and sometime before the next sequence point,
increment i.
++i: evaluate to i+1, and sometime before the next sequence point,
increment i.

You can't make any assumptions about when the side effect is actually
applied, other than it must be before the next sequence point. The
second case can evaluate as

tmp <- Ri + 1
Ri <- Ri + 1
Li <- tmp

or

tmp <- Ri + 1
Li <- tmp
Ri <- Ri + 1

or

Ri <- Ri + 1
Li <- Ri

or even something else entirely, as long as the semantics are
preserved.
I don't think there will be any problem in this second case.


The second case has the exact same problem as the first case, at least
if you're dealing with scalar types. "i = ++i" is just as undefined as
"i = i++".

Mar 22 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Laphan | last post by:
Hi All I'm using .getRows() with a local var array instead of doing a recursive loop so that I'm being a good ASP newvbie and closing my object i/o's (the recordset in this case) as quick as...
3
by: R2 | last post by:
Hello Can anyone advise / help on this problem. I have a friend (brother in law) who is in the (spam) telephone fax business. He would like to convert numerous phone lists from old data bases to...
2
by: Ravi Sankar | last post by:
Hi, I have been working in quite a big project that is split into two phases. The phase I part of ASP.Net web application is live and under maintenace now. We have started design of phase II....
3
by: Andy Sutorius | last post by:
Hi, I read the thread (2/16/05) regarding a replace function in C# however it didn't answer my question. I have a string which is building an insert sql statement and I would like to replace...
4
by: Tiraman | last post by:
Hi, i have some problem to run my dll's (assemblies) on the production server. I have 3 dll's which use each other. Every thing is ok on my local machine where i developed them but when i...
0
by: vve | last post by:
I'm discovering a strange behaviour in an C# project using ZedGraph (https://sourceforge.net/projects/zedgraph/). After adding a signal to it, it seems that the clr goes mad for some reason. I...
10
by: John Passaniti | last post by:
(Note: This is not the same message I posted a week or so ago. The problem that prevented my previous attempt to work was a silly error in the template system I was using. This is a problem...
27
by: Joe | last post by:
I have a string parsing function which uses above code. i and ii are same at first but as my loop goes on ii is incremented more. I could do if (ii!=i) mystr=mystr; but I want to limit...
4
by: jiatiejun | last post by:
I want to convert a path from ~/xxx/xxx.gif to 'http://xxxxxxx/xxx/xxx.gif' how to convert it? thanks btw: the convert function allow
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.