简体   繁体   English

为什么 C/C++ 没有定义表达式求值顺序?

[英]Why C/C++ does not defined expression evaluation order?

As you may know C/C++ does not specified expression evaluation order.您可能知道 C/C++ 没有指定表达式求值顺序。 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.一个例子是重新排序算术指令以最大限度地使用 ALU 或隐藏计算的内存延迟。

One of the C/C++ design goals is the efficient implementation for complilers. C/C++ 设计目标之一是编译器的高效实现。 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来源:Steve Summit 的 C 编程常见问题解答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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