473,387 Members | 1,541 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,387 software developers and data experts.

Is it standard C++ code?

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
4 1397
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Magnus Lie Hetland | last post by:
Is there any interest in a (hypothetical) standard graph API (with 'graph' meaning a network, consisting of nodes and edges)? Yes, we have the standard ways of implementing graphs through (e.g.)...
1
by: Corrie Meyer | last post by:
Announcement: SwiftReports standard edition 1.0 for Visual Studio ..NET 2003 released by UniSwift. We are pleased to announce the first release of a fully-managed reporting tool for the...
43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
52
by: lovecreatesbeauty | last post by:
Why the C standard committee doesn't provide a standard implementation including the C compiler and library when the language standard document is published? C works on the abstract model of low...
132
by: Frederick Gotham | last post by:
If we look at a programming language such as C++: When an updated Standard comes out, everyone adopts it and abandons the previous one. It seems though that things aren't so clear-cut in the C...
21
by: Kannan | last post by:
Its been a while I have done pure C programming (I was coding in C++). Is the following function valid according to standard C? int main(int argc, char *argv) { int x; x = 9; printf("Value...
7
by: Lighter | last post by:
Is overriding a function of a library in accordance with C++ standard? The following code are passed by the VS 2005 and Dev C++. #include <cstdlib> #include <iostream> using namespace std;...
56
by: Chris Hills | last post by:
Hi, It came up in a standards panel meeting the other day that "all c or C++ programmers" have a copy of ISO C and/or C++ ... I challenged this and said most don't (outside those working on...
85
by: fermineutron | last post by:
Some compilers support __asm{ } statement which allows integration of C and raw assembly code. A while back I asked a question about such syntax and was told that __asm is not a part of a C...
20
by: Pilcrow | last post by:
This behavior seems very strange to me, but I imagine that someone will be able to 'explain' it in terms of the famous C standard. -------------------- code -----------------------------------...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.