By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,445 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,445 IT Pros & Developers. It's quick & easy.

Is it standard C++ code?

P: n/a
i found something wrong code.

int i = 0;
i = i++;

is it standard c++? peoples say i == 1 in most compilers but anothers
not the same. if so, may be it's not standard code, however i want know
positively.

Nov 11 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ljh131 wrote:
i found something wrong code.

int i = 0;
i = i++;

is it standard c++? peoples say i == 1 in most compilers but anothers
not the same. if so, may be it's not standard code, however i want know
positively.

It's undefined behavior. You are attempting to change a variable
twice between sequence points (once for the assignment and once
for the side-effect of ++).

Nov 11 '05 #2

P: n/a
"ljh131" <lj****@gmail.com> schrieb im Newsbeitrag
news:11**********************@g49g2000cwa.googlegr oups.com...
i found something wrong code.

int i = 0;
i = i++;

is it standard c++? peoples say i == 1 in most compilers but anothers
not the same. if so, may be it's not standard code, however i want know
positively.


Yes, it is standard C++, but the behaviour is not defined by the standard.
In the statement 'i = i++', there is no sequence point and i is modified
more than once. Many compilers might create code such that finaly i == 1,
but it is easy to imagine a sequence of code, that results in i == 0:

1. Evaluate the expression i
2. Increment i
3. Assign the result of step 1 to i

So remember what they tell the kids in TV -- "Don't do that at work"

HTH
Heinz
Nov 11 '05 #3

P: n/a
ljh131 wrote:
i found something wrong code.

int i = 0;
i = i++;

is it standard c++?
Yes, in the sense that it will compile. However, the behaviour of
that code is undefined, according to the Standard. So, the usual
advice is: don't do that.
peoples say i == 1 in most compilers but anothers
not the same. if so, may be it's not standard code, however i want know
positively.


If the behaviour is undefined, there is nothing we can say about the
outcome of executing that code from the Standard C++ point of view.

V
Nov 11 '05 #4

P: n/a
In article <43***********************@newsread2.arcor-online.net>,
Heinz Ozwirk <ho**********@arcor.de> wrote:
"ljh131" <lj****@gmail.com> schrieb im Newsbeitrag
news:11**********************@g49g2000cwa.googleg roups.com...
i found something wrong code.

int i = 0;
i = i++;

is it standard c++? peoples say i == 1 in most compilers but anothers
not the same. if so, may be it's not standard code, however i want know
positively.


Yes, it is standard C++, but the behaviour is not defined by the standard.
In the statement 'i = i++', there is no sequence point and i is modified
more than once. Many compilers might create code such that finaly i == 1,
but it is easy to imagine a sequence of code, that results in i == 0:

1. Evaluate the expression i
2. Increment i
3. Assign the result of step 1 to i

So remember what they tell the kids in TV -- "Don't do that at work"


Right, the problem is whether the ++ side-effect takes place
before or after the assignment. But we don't get only those
two choices (which is bad enough) and hence as you say it is
undefined behavior.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 11 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.