Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is it standard C++ code?

Reply
Thread Tools

Is it standard C++ code?

 
 
ljh131
Guest
Posts: n/a
 
      11-11-2005
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.

 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      11-11-2005
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 ++).

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-11-2005
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
 
Reply With Quote
 
Heinz Ozwirk
Guest
Posts: n/a
 
      11-11-2005
"ljh131" <> schrieb im Newsbeitrag
news: 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


 
Reply With Quote
 
Greg Comeau
Guest
Posts: n/a
 
      11-11-2005
In article <4374d35c$0$21945$>,
Heinz Ozwirk <> wrote:
>"ljh131" <> schrieb im Newsbeitrag
>news: 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
standard libraries don't behave like standard 'libraries' Sriram Srinivasan Python 13 11-12-2009 06:05 PM
What are the standard network functions provided in standard C? disappearedng@gmail.com C Programming 5 06-10-2008 08:57 PM
How to redirect a "system" standard output and standard error to avariable (Linux) Venks Ruby 5 12-06-2007 12:21 AM
add pexpect to the standard library, standard "install" mechanism. funkyj Python 5 01-20-2006 08:35 PM
How standard is the standard library? steve.leach Python 1 04-18-2005 04:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57