问题描述
时,比如为(;;)的无限循环;
用C未定义行为? (这,但我不知道关于C)
Is an infinite loop like for (;;);
undefined behavior in C? (It is for C++, but I don't know about C.)
推荐答案
没有,的行为为(;;)
语句在C明确界定。
No, the behavior of a for (;;)
statement is well defined in C.
时,这是对官方2011的ISO基本相同C标准说,在第6.8.5条第6款:
N1570, which is essentially identical to the offical 2011 ISO C standard, says, in section 6.8.5 paragraph 6:
这是循环语句的控制前pression不是一个常数
前pression,即不执行输入/输出操作,不访问
挥发物,并且不执行同步或原子操作
在它的身上,控制前pression,或(在的情况下的为
语句)它的前pression-3 的,可由执行假设
终止。
有两个脚注:
这是省略控制前pression由非零常数取代,
这是一个不断前pression。
这是为了让编译器转换,如去除
即使终端无法证明空循环。
This is intended to allow compiler transformations such as removal of empty loops even when termination cannot be proven.
第一个脚注明确指出,为(;;)
作为如果它有一个恒定的控制前pression处理。
The first footnote makes it clear that for (;;)
is treated as if it had a constant controlling expression.
该规则的要点是允许优化时,编译器不能证明循环终止。但如果控制前pression是恒定的,编译器的可以的平凡证明该环或不终止,所以不需要额外的权限。
The point of the rule is to permit optimizations when the compiler can't prove that the loop terminates. But if the controlling expression is constant, the compiler can trivially prove that the loop does or does not terminate, so the additional permission isn't needed.
这篇关于是一个(空的)无限循环不确定的用C的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!