post and pre increment operator

This is a discussion on post and pre increment operator within the C Programming forums, part of the General Programming Boards category; I was learning sequence point and I get really confused by example given in wikipedia that "i=i++" is undefined, Can ...

  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
    21,717
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,287
    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%.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,287
    Yes.
    Right 98% of the time, and don't care about the other 3%.

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, 03: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, 04: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21