是一个(空的)无限循环不确定的用C的行为? [英] Is an (empty) infinite loop undefined behavior in C?

查看:171
本文介绍了是一个(空的)无限循环不确定的用C的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时,比如为(;;)的无限循环; 用C未定义行为? (这是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.

N1570 时,这是对官方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 的,可由执行假设
  终止。

An iteration statement whose controlling expression is not a constant expression, that performs no input/output operations, does not access volatile objects, and performs no synchronization or atomic operations in its body, controlling expression, or (in the case of a for statement) its expression-3, may be assumed by the implementation to terminate.

有两个脚注:

这是省略控制前pression由非零常数取代,
  这是一个不断前pression。

An omitted controlling expression is replaced by a nonzero constant, which is a constant expression.

这是为了让编译器转换,如去除
  即使终端无法证明空循环。

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的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆