简体   繁体   中英

Why C/C++ does not defined expression evaluation order?

As you may know C/C++ does not specified expression evaluation order. What are the reasons to left them undefined.

It allows compiler optimizations. One example would be the reordering of arithmetic instructions for the maximum usage of the ALUs or hiding memory latency with calculations.

One of the C/C++ design goals is the efficient implementation for complilers. So the compilers are given relatively free rein in choosing the evaluation order of various subexpression within a complicated expression; this order is not constrained by operator precedence and associativity as we think. In such situation when we a modifying the same vairiable in multiple sub expression, the behaviour become undefied. An increment or decrement operation is not guaranteed to be performed immediately after giving upthe previous value and before any other part of the expression is evalauted. The only guarantee is that the update will be performed before the expression is considered finished.

Undefined behavious means undefined and anything can happen .

Source:C programmig FAQ by Steve Summit

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