CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    Please help me understand the ouput.

    Here is the code,
    Code:
    int i = 1;
    i = i++;
    At the end, i is still 1 instead of 2,why? Thanks.

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Please help me understand the ouput.

    Undefined behavior... look up "sequence points"

    http://en.wikipedia.org/wiki/Sequence_point

    See 2nd paragraph.

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: Please help me understand the ouput.

    Quote Originally Posted by Philip Nicoletti View Post
    Undefined behavior... look up "sequence points"
    I have a feeling it's defined in C++ 11. At least that's how I interpret section 5.2.6 in the standard: "The value computation of the ++ expression is sequenced before the modification of the operand object". In that case i = i++ is equivalent to this,
    Code:
    int eval = i; // i++ is evaluated before incrementation
    i++; // i is incremented
    i = eval // the assignment is performed
    But it's still bad programming. If an outcome surprises you it probably surprises most programmers so it should be avoided.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Please help me understand the ouput.

    Quote Originally Posted by nuzzle
    At least that's how I interpret section 5.2.6 in the standard: "The value computation of the ++ expression is sequenced before the modification of the operand object".
    I think that just establishes the "post-increment/decrement" aspect, i.e., the value of i++ is guaranteed to be the value of i since that is sequenced before the change to i. It does not mean that the increment operation is sequenced before the assignment operation in the code in post #1.

    In other words, I think that the wording would still allow a conforming compiler to translate it as if it were:
    Code:
    int eval = i; // i++ is evaluated before incrementation
    i = eval // the assignment is performed
    i++; // i is incremented
    Last edited by laserlight; June 5th, 2013 at 01:15 AM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: Please help me understand the ouput.

    Quote Originally Posted by laserlight View Post
    In other words, I think that the wording would still allow a conforming compiler to translate it as if it were:
    Well, it looks like you're right. VS2012 actually outputs 2.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Please help me understand the ouput.

    Heh, thanks for the confirmation, but I just remembered: we still have a similiar rule in place, so the behaviour is not just implementation defined in C++11, but still undefined as in C++03:
    Quote Originally Posted by C++11 Clause 1.9 Paragraph 15 (part)
    If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured