unexpected result

Discussion in 'C++' started by Jim Hester, Mar 7, 2006.

  1. Jim Hester

    Jim Hester Guest

    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;
     
    Jim Hester, Mar 7, 2006
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Mar 7, 2006
    #2
    1. Advertising

  3. Jim Hester

    Default User Guest

    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
     
    Default User, Mar 7, 2006
    #3
  4. Jim Hester

    red floyd Guest

    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.
     
    red floyd, Mar 7, 2006
    #4
  5. Jim Hester

    Ben Pope Guest

    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...
     
    Ben Pope, Mar 8, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Pemberton

    unexpected result using std::list

    Mike Pemberton, Oct 14, 2003, in forum: C++
    Replies:
    3
    Views:
    356
    Dan Cernat
    Oct 14, 2003
  2. John J

    Unexpected Result

    John J, May 12, 2004, in forum: C++
    Replies:
    8
    Views:
    395
    John J
    May 13, 2004
  3. japh
    Replies:
    4
    Views:
    784
  4. Michael Tan
    Replies:
    32
    Views:
    1,083
    Ara.T.Howard
    Jul 21, 2005
  5. Andy Tolle
    Replies:
    7
    Views:
    252
    Andy Tolle
    Nov 15, 2010
Loading...

Share This Page