Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > unexpected result

Reply
Thread Tools

unexpected result

 
 
Jim Hester
Guest
Posts: n/a
 
      03-07-2006
The following stemmed from a slightly bizarre line in a student's work that
surprised my by doing what HE wanted it to do. Question: Are the second and
third cases below correct by some interpretation of the language semantics
that I'm missing, or is it some kind of register-optimization bug (or
something else)?

int i = 1;

int j;

j = i++; // i ends up 2, j ends up 1 (as expected)

cout << "i=1; j = i++; i == " << i << " j == " << j << endl << endl;



int k = 1;

k = k++; // k ends up 2 - not exptectd.

cout << "K=1; k = k++; k == " << k << endl << endl;



int m = 1;

int p;

m = ( p = m++ ); // p ends up 1 as expected, but m remains 2 (not set back
to 1)

cout << "m=1; m = (p = m++); p == " << p << " m == " << m << endl << endl;


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-07-2006
Jim Hester wrote:
> The following stemmed from a slightly bizarre line in a student's work that
> surprised my by doing what HE wanted it to do. Question: Are the second and
> third cases below correct by some interpretation of the language semantics
> that I'm missing, or is it some kind of register-optimization bug (or
> something else)?
>
> [... i=i++ again ...]


Search in the news archives for "undefined behaviour".

V
--
Please remove capital As from my address when replying by mail
 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      03-07-2006
Jim Hester wrote:


> int k = 1;
>
> k = k++; // k ends up 2 - not exptectd.
>
> cout << "K=1; k = k++; k == " << k << endl << endl;



Not sure if the C++ FAQ covers it, but it's the same as in C:

http://c-faq.com/expr/ieqiplusplus.html


Brian
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      03-07-2006
Jim Hester wrote:
>
> k = k++; // k ends up 2 - not exptectd.
>
> m = ( p = m++ ); // p ends up 1 as expected, but m remains 2 (not set back
> to 1)
>


Both of these invoke undefined behavior. It is free to work "as
expected", or do anything else, including blow up your monitor, reformat
your hard drive, or even contact the Pentagon and start WWIII.
 
Reply With Quote
 
Ben Pope
Guest
Posts: n/a
 
      03-08-2006
Jim Hester wrote:
> The following stemmed from a slightly bizarre line in a student's work that
> surprised my by doing what HE wanted it to do. Question: Are the second and
> third cases below correct by some interpretation of the language semantics
> that I'm missing, or is it some kind of register-optimization bug (or
> something else)?
>
> int i = 1;
>
> int j;
>
> j = i++; // i ends up 2, j ends up 1 (as expected)
>
> cout << "i=1; j = i++; i == " << i << " j == " << j << endl << endl;
>
>
>
> int k = 1;
>
> k = k++; // k ends up 2 - not exptectd.


This is undefined behaviour. Search for "sequence point".

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
 
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
Mysql::Result .each_hash - unexpected result Andy Tolle Ruby 7 11-15-2010 10:39 AM
1. Ruby result: 101 seconds , 2. Java result:9.8 seconds, 3. Perl result:62 seconds Michael Tan Ruby 32 07-21-2005 03:23 PM
Unexpected result when entering wrong data type japh C++ 4 01-08-2005 08:21 PM
Unexpected Result John J C++ 8 05-13-2004 08:48 AM
unexpected result using std::list Mike Pemberton C++ 3 10-14-2003 06:24 PM



Advertisments