Thread: timming of ++ incremment

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

    timming of ++ incremment

    Code:
    array[i++] = 5;
    Code:
    array[i++] = //super long expression
    I know first one works fine now with second i was never confident so up to now i played it safe by doing i++ on separate statement.
    I'd like to clear this once and for all. When does the increment of i actually happen? Is it safe to expect that the value of i used to store something in array will always be i before increment?

  2. #2
    Guest
    Guest
    i++ means evaluate current i, then increment it. You can test this by printing it out.
    Whenever you want to do a regular (immediate) increment, you should use ++i, which makes the intent clear.

    p.s. doing the increment on its own line might still be a good idea in terms of clarity. Compact code is only more readable up to a point.
    Last edited by Guest; 12-24-2015 at 06:58 PM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by telmo_d
    When does the increment of i actually happen? Is it safe to expect that the value of i used to store something in array will always be i before increment?
    This is what the C standard has to say:
    Quote Originally Posted by C11 Clause 6.5.2.4 Paragraph 2 (part)
    The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it). (...) The value computation of the result is sequenced before the side effect of updating the stored value of the operand.
    That said, you need to be aware that a statement like this results in undefined behaviour:
    Code:
    array[i++] = i;
    The third sentence of what I quoted from the C standard refers only to i++, i.e., the result of i++ is i before the side-effect of the increment. However, the appearance of i in another place in the expression is not covered by this, and so another part of the C standard comes into play:
    Quote Originally Posted by C11 Clause 6.5 Paragraph 2a
    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.
    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. Timming algorithm
    By Mario in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2001, 11:35 AM