
An actual implementation need not evaluate part of an Expression if it can deduce that its
value is not used and that no needed sIDe effects are produced (including any caused by
calling a function or accessing a volatile object).
假设我有以下代码:
char data[size];int i;int found;/* initialize data to some values in here */found = 0;for( i = 0; i < size; i++ ) { if( data[i] == 0 ) { found = 1; /* no break in here */ }}/* i no longer used,do something with "found" here */ 请注意,found从0开始,可以保持不变或变为1.它不能变成1然后变成其他东西.所以下面的代码会产生相同的结果(除了i值之外,无论如何都不会在循环之后使用):
char data[size];int i;int found;/* initialize data to some values in here */found = 0;for( i = 0; i < size; i++ ) { if( data[i] == 0 ) { found = 1; break; }}/* i no longer used,do something with "found" here */ 现在标准所说的不需要评估关于found = 1的表达式的一部分以及第一次迭代之后的循环控制表达式,其中控制进入内部,如果?
显然,如果在此代码之后的某处使用found,则编译器必须发出遍历数组的代码并有条件地计算found = 1表达式.
对于在数组中找到的每个零,是否需要对find = 1进行一次评估,或者它是否可以将其评估为一次,因此在编译第一个片段时有效地为第二个片段发出代码?
解决方法can it instead evaluate it no more that once and so effectively emit the code for the second snippet when compiling the first snippet?
是的,编译器有权执行该优化.这似乎是一个非常积极的优化,但它是合法的.
看一个更符合文本精神的例子可能会很有趣:
An actual implementation need not evaluate part of an Expression if it can deduce that its value is not used and that no needed sIDe effects are produced (including any caused by calling a function or accessing a volatile object).
假设我们有:
int x = pureFunction(y) * otherPureFunction(z);
假设编译器知道两个函数都是int返回“纯”函数;也就是说,它们没有副作用,它们的结果完全取决于论点.假设编译器还认为其他PureFunction是一个非常昂贵的 *** 作.编译器可以选择像您编写的那样实现代码:
int temp = pureFunction(y);int x = temp == 0 ? 0 : temp * otherPureFunction(z);
也就是说,确定在某些条件下没有必要计算otherPureFunction(),因为一旦知道左 *** 作数为零,乘法的结果就已经知道了.没有必要的副作用将被省略,因为没有副作用.
总结以上是内存溢出为你收集整理的C99 Standard是否允许编译器转换代码,以便在满足某些推断条件后不再评估相同的表达式?全部内容,希望文章能够帮你解决C99 Standard是否允许编译器转换代码,以便在满足某些推断条件后不再评估相同的表达式?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)