unexpected result

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Hester

    #1

    unexpected result

    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;


  • Victor Bazarov

    #2
    Re: unexpected result

    Jim Hester wrote:[color=blue]
    > 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 ...][/color]

    Search in the news archives for "undefined behaviour".

    V
    --
    Please remove capital As from my address when replying by mail

    Comment

    • Default User

      #3
      Re: unexpected result

      Jim Hester wrote:

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


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




      Brian

      Comment

      • red floyd

        #4
        Re: unexpected result

        Jim Hester wrote:[color=blue]
        >
        > 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)
        >[/color]

        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.

        Comment

        • Ben Pope

          #5
          Re: unexpected result

          Jim Hester wrote:[color=blue]
          > 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.[/color]

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

          Ben Pope
          --
          I'm not just a number. To many, I'm known as a string...

          Comment

          Working...