简体   繁体   中英

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,

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM