Homero
Homero

Reputation: 11

C++ compiler questions regarding increment operators

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,

Upvotes: 0

Views: 108

Answers (1)

user3344003
user3344003

Reputation: 21607

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.

Upvotes: 3

Related Questions