-3

I had come across this answer to this question. In one of the line, the author mention's:

In any case, follow the guideline "prefer ++i over i++" and you won't go wrong.

I know that ++i is slightly faster than i++, but thought that there is no reason for them to go wrong. I searched for a while, and the closest I could get to was this. It explained clearly that why is it preferred to use ++i, but not still how could you go wrong using i++.

So can anyone tell me how can i++ go wrong?

NOTE: My question isn't a dupe, as I am not asking about the performance. I have already seen that question. I am asking how can i++ be wrong as mentioned in the answer I have mentioned above.

Community
  • 1
  • 1
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
  • 4
    It's not "slightly faster"; anyone who tells you this has no idea what they're talking about. – R.. GitHub STOP HELPING ICE Feb 26 '16 at 05:17
  • @R.., but you that is even written in the question that I've linked. It is written that it is faster because, `i++` has to store the old value, while `++i` does not. So the time storing it is saved. See: [Is there a performance difference between i++ and ++i in C?](http://stackoverflow.com/a/24887/5735775) – Box Box Box Box Feb 26 '16 at 05:19
  • 4
    @AshishAhuja While historically there were minor performance differences, modern compilers are so good at spotting and eliminating dead code that I would be astonished if this actually was an issue anymore. – templatetypedef Feb 26 '16 at 05:21
  • 2
    @AshishAhuja: The claim is wrong. There is no "storing the old value" taking place. – R.. GitHub STOP HELPING ICE Feb 26 '16 at 05:26
  • Even if it's not about performance, I think the (logical) difference between `++i` and `i++` must have been discussed a gazillion times... – Nicolas Miari Feb 26 '16 at 05:29
  • 1
    @AshishAhuja The answer you quoted also says *"++i seems more common, perhaps because that is what is used in K&R."* But in fact, a quick look at K&R second edition shows that the book uses both styles, with `i++` being the more common. And `i++` is used in section 3.5 where the `for` loop is formally introduced. So I think the answer you quoted is a little biased, and the author is just promoting his preferred style. – user3386109 Feb 26 '16 at 05:46
  • 1
    @Barmar this isn't a dupe. – Qix - MONICA WAS MISTREATED Feb 26 '16 at 05:55
  • *"I am asking how can i++ be wrong as mentioned in the answer I have mentioned above."* The only person that can answer your question is Mark Harrison, since he wrote the answer that you quoted. He also wrote the question and answer that Barmar cited. In that question and answer, he not only said that there's no performance difference, but also that the compiler generates identical code. So this question should have been asked as a comment under his answer. – user3386109 Feb 26 '16 at 06:15
  • By "go wrong" the person who wrote the post obviously meant "write inefficient code". So yes, this is a duplicate, even if you might not realize. – Lundin Feb 26 '16 at 07:26
  • 4
    Generally the whole "++i is faster" myth originates from C++, where it is sometimes true, since that language has operator overloading. – Lundin Feb 26 '16 at 07:32
  • @Lundin, how is operator overloading related to post and pre increment? – Box Box Box Box Feb 26 '16 at 07:33
  • @AshishAhuja Because if you overload the postfix ++ operator in C++, there will be a function call and inside it, a temporary copy of the "this" object is created. [Code example](http://stackoverflow.com/a/1392850/584518). – Lundin Feb 26 '16 at 07:37
  • 1
    Actually array[i++] may be slightly faster than array[++i]. The reason is that on superscalar (e.g. modern Intel) CPUs the increment and address calculation can be done in parallel with the former, but not with the later. This is called instruction level parallelism. – Eloff Feb 26 '16 at 18:04
  • 2
    @Eloff: That's false. `array[++i]` is equivalent to `(array+1)[i++]` and the latter is equally capable of being done in parallel. – R.. GitHub STOP HELPING ICE Mar 04 '16 at 15:35

1 Answers1

5

In the case of

for (i=start; i<end; i++)

vs

for (i=start; i<end; ++i)

their meanings are completely identical, because the value of the expressions i++ and ++i are not used. (They are evaluated for side effects only.) Any compiler which produces different code for the two is pathologically bad and should be chucked in the garbage. Here, use whichever form you prefer, but i++ is idiomatic among C programmers.

In other contexts, use the form that yields the value you want. If you want the value from before the increment, use i++. This makes sense for things like:

index = count++;

or

array[count++] = val;

where the old value of count becomes the index of a new slot you're going to store something in.

On the other hand, if you want the value after increment, use ++i.

Note that for integer types, the following are all completely equivalent:

  • i += 1
  • ++i
  • i++ + 1

and likewise the following are equivalent:

  • (i += 1) - 1
  • i++
  • ++i - 1

For floating point and pointers it's more complicated.

As far as any objective reasons are concerned, the advice you found is just nonsense. If on the other hand you find ++i easier to reason about, then feel free to take the advice, but this is going to depend entirely on the individual and what you're used to.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • You clear all my doubts. just that you mean to say that @MarkHarrison is wrong, and actually there is no difference between `i++` and `++i`? – Box Box Box Box Feb 26 '16 at 06:30
  • @AshishAhuja This answer is pretty much identical to [this one](http://stackoverflow.com/a/26482954/584518) posted by me in the linked duplicate. – Lundin Feb 26 '16 at 07:30