Thread: Undefined behavior

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    10

    Undefined behavior

    Hi,


    Why does the following code exhibit undefined behavior?
    Code:
    int i = 1;
    i += ++i;

    Is it because of the following rule?
    Between consecutive "sequence points" an object's value can be modified only once by an expression.

    I think that it shouldn't be undefined, because:
    1) Since ++ has higher precedence than +=, the increment operator is first evaluated. So ++i increments i by one and returns the new value of i, that is 2.
    2) Then += is evaluated. The left operand is an object (having number 2) and the right operand is 2. So the sum, of the content of the object and two, is finally stored in the object (that is i).


    It's however true that the object's value is modified more than once between the two consecutive sequence points.


    Thanks for help...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,279
    Quote Originally Posted by atran
    I think that it shouldn't be undefined, because:
    1) Since ++ has higher precedence than +=, the increment operator is first evaluated. So ++i increments i by one and returns the new value of i, that is 2.
    2) Then += is evaluated. The left operand is an object (having number 2) and the right operand is 2. So the sum, of the content of the object and two, is finally stored in the object (that is i).
    Precedence determines grouping, not order of evaluation. Another plausible interpretation is that on the left hand side of the +=, the i is evaluated in order to determine the result of the += expression, then ++i is evaluated. Therefore, the final value of i should obviously be 3, not 4.

    That said, this alternative order of evaluation does not matter because...

    Quote Originally Posted by atran
    It's however true that the object's value is modified more than once between the two consecutive sequence points.
    ... the behaviour is undefined.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    10
    Quote Originally Posted by laserlight View Post
    Precedence determines grouping, not order of evaluation.
    Thanks.


    Is i=++i defined? If so, why?


    Also, does an assignment expression return the value assigned to the left operand, or does it instead return the value of the left operand after the actual assignment?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,279
    Quote Originally Posted by atran
    Is i=++i defined? If so, why?
    It results in undefined behaviour for the same reason as in post #1.

    Quote Originally Posted by atran
    Also, does an assignment expression return the value assigned to the left operand, or does it instead return the value of the left operand after the actual assignment?
    Refer to the C standard:
    Quote Originally Posted by C11 Clause 6.5.16 Paragraph 3
    An assignment operator stores a value in the object designated by the left operand. An assignment expression has the value of the left operand after the assignment, but is not an lvalue. The type of an assignment expression is the type the left operand would have after lvalue conversion. The side effect of updating the stored value of the left operand is sequenced after the value computations of the left and right operands. The evaluations of the operands are unsequenced.
    Incidentally, speaking of C11, the rule that you mentioned in post #1 has been updated to:
    Quote Originally Posted by C11 Clause 6.5 Paragraph 2
    If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined behavior
    By Ducky in forum C Programming
    Replies: 14
    Last Post: 11-17-2013, 07:10 AM
  2. Is this not undefined behavior?
    By Syscal in forum C Programming
    Replies: 6
    Last Post: 07-15-2013, 01:07 AM
  3. Undefined behavior
    By jim mcnamara in forum C Programming
    Replies: 2
    Last Post: 02-18-2013, 11:14 PM
  4. Is x=x++; Undefined Behavior?
    By envec83 in forum C Programming
    Replies: 5
    Last Post: 10-04-2011, 01:27 AM
  5. Undefined behavior from VC6 to 2k5
    By m37h0d in forum C++ Programming
    Replies: 10
    Last Post: 06-22-2011, 07:56 PM

Tags for this Thread