Why is it considered bad practice to use a prefix increment operator versus a postfix operator with old compilers? By my understanding modern compilers optimize this but old ones do not. Was there a particular reason that one was slower than the other, and a reason that it was left optimized in compiled code? If you could provide any other details regarding operators and compilers I'd be grateful, thanks,
Asked
Active
Viewed 92 times
0
-
6Is there a reference of *it's bad practice to use a prefix increment operator versus a postfix operator with old compilers*? – Yu Hao Jul 11 '15 at 03:51
-
3Related question and answer: http://stackoverflow.com/questions/17436141/kr-seems-to-prefer-pre-increment/17458012#17458012 – Jens Jul 11 '15 at 03:52
-
1Did you really want to say that `++p` is *worse* than `p++`? Usually there is either no difference or (in specific situations, e.g. using overloaded operators) the `p++` is slower. – dlask Jul 11 '15 at 04:21
-
Postincrement/postdecrement copies the object. This won't make any difference for built-in types, though, or even for overloaded operators on enums - it only matters for overloaded operators on class types. – celticminstrel Jul 11 '15 at 05:53
1 Answers
3
By "old" you must mean VERY OLD.
The reason was, back in the days before real optimization, the main system for C/Unix development was the PDP-11 and later VAX-11. The PDP and VAX have pre-decrement and post increment addressing modes.
MOVL R0, -(R1) ; Decrements R1 by 4 and move the value of R0 to that location.
MOVL (R1)+, R2 ; Move the value at R1 to R2 then add 4 to R1.
These would then be the C equivalent of
*(--a) = b ;
b = *(a++) ;
In the days of little optimization, these could easily be mapped back to the underlying assembly instructions.
At the same time these addressing modes did not exist to do the opposite:
MOVL (R0)-, R1
MOVL +(R0), R1
Therefore, there was no hardware mapping to
*(++a) = b ;
b = *(a--) ;
The -(Rx) and (Rx)+ addressing modes made it easy to implement downwards growing stacks.

user3344003
- 20,574
- 3
- 26
- 62