Thread: post and pre increment operator

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    28

    post and pre increment operator

    I was learning sequence point and I get really confused by example given in wikipedia that "i=i++" is undefined,

    Can anybody explain me why it is so ? I can understand that "a[i] = i++" is undefined but I can not figure out why i = i++ is,

    Second question is--
    Is "a[i] = ++i;" is also undefined

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Here's the rule:
    Quote Originally Posted by C99 Clause 6.5 Paragraph 2
    Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.
    So, when we look at this statement:
    Code:
    i = i++;
    We observe that there is a sequence point before and after this statement. We also notice that i++ modifies i, but the assignment to i also modifies i. Therefore, i has its stored value modified more than once by the evaluation of the expression in this statement, leading to undefined behaviour.

    Quote Originally Posted by _arjun
    Second question is--
    Is "a[i] = ++i;" is also undefined
    What do you think?
    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
    Sep 2011
    Posts
    28
    Thanks you very much for such a clear answer .

    Regarding second question I think it should be well defined, because I have read this at many places that "++i" will "first increment the value of i and then return the incremented value of i".

    Therefore,
    a[i] = ++i;
    should write incremented value of i at incremented index of a, but the problem is that by going this logic "i = ++i;" also seems well defined, which is certainly not true according to the rule you have mentioned.
    So please tell me where am I going wrong.

    Thanks again

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The C standard states that any expression (essentially: code between two sequence points, as described by laserlight) that modifies a variable twice invokes undefined behaviour.

    How many times, in total, can the expression "a[i] = i++" modify i? How many times, in total, can the expression "i = ++i" modify i?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    But that still doesn't get you off the hook as to whether a[i] is evaluated before OR after ++i is evaluated.

    The compiler is free to do something like
    int *answerGoesHere = &a[i];
    ++i;
    *answerGoesHere = i;


    Or it could evaluate answerGoesHere after the increment.
    Either way, the code is broken.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by _arjun
    Regarding second question I think it should be well defined, because I have read this at many places that "++i" will "first increment the value of i and then return the incremented value of i".

    Therefore,
    a[i] = ++i;
    should write incremented value of i at incremented index of a, but the problem is that by going this logic "i = ++i;" also seems well defined, which is certainly not true according to the rule you have mentioned.
    So please tell me where am I going wrong.
    This is why I quoted the full paragraph of the rule
    ++i causes i to be read and modified, therefore by the second sentence, "the prior value shall be read only to determine the value to be stored". However, a[i] also causes the value of i to be read, not to determine the value to be stored into i, but to determine which element of a should be modified, thus violating this rule.

    Salem gave a practical explanation of what is likely to happen, but note that this is not just implementation defined behaviour such that the compiler might choose between one of two options. Rather, 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

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Thanks got it

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Just confirming

    is expression

    i = i = 1;

    also undefined because it also modifies the object more than once between sequence point

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Yes.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pre & post increment need help
    By zing_foru in forum C Programming
    Replies: 6
    Last Post: 10-09-2009, 12:14 PM
  2. pre/Post Increment
    By ganesh bala in forum C Programming
    Replies: 1
    Last Post: 02-12-2009, 04:17 AM
  3. post increment gives trouble !
    By samirself in forum C Programming
    Replies: 3
    Last Post: 05-07-2005, 03:11 PM
  4. post vs pre increment
    By xddxogm3 in forum C Programming
    Replies: 13
    Last Post: 03-19-2004, 05:07 PM
  5. Post increment and pre increment help
    By noob2c in forum C++ Programming
    Replies: 5
    Last Post: 08-05-2003, 03:03 AM

Tags for this Thread