实现无限循环时,使用 while(1) 与 for(;;) 与 goto (在 C 中)有区别吗?
实现无限循环时,使用 while(1)
与 for(;;)
与 goto
有区别吗?
谢谢, 陈兹
When implementing an infinite loop, is there a difference in using while(1)
vs for(;;)
vs goto
?
Thanks,
Chenz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
即使您关闭优化器,它们也是等效的。
示例:
使用
gcc -O0
编译给出所有 3 个函数的等效汇编:They are equivalent, even if you turn the optimizer off.
Example:
Compile with
gcc -O0
gives equivalent assembly for all 3 functions:我只是比较了 gcc 未优化的汇编器输出:
生成汇编器输出:
比较汇编器文件:
正如您所看到的,没有显着差异。这是输出,
虽然这不是它们相同的技术证明,但我想说在 99.9% 的情况下都是相同的。
I just compared the unoptimized assembler output of gcc:
Make assembler output:
Compare assembler files:
As you can see there are no significant differences. Here is the output
While this is not a technical proof that they are the same, I'd say it is in 99.9% of the cases.
生成的程序集几乎没有任何区别。这更多的是一个风格问题:
Goto - 只是 ooogly:向后跳转,没有明确的无限块
while(1) - 更好,但需要“虚拟”条件,你会经常被编译器警告(警告级别 4)或静态分析工具
for(;;) 可能不是最漂亮的,但恕我直言,最适合,因为这个构造不能有任何其他含义(与 while 相比)。但其他一些人出于“相同”的原因更喜欢 while(1) ......
There is hardly any difference in generated assembly. It's more of an stylistic issue:
Goto - just ooogly: jumps backward, no explicit infinite block
while(1) - better, requires "dummy" condition though and you'll be often warned by compiler(warning level 4) or static analysis tool
for(;;) might not be the prettiest, but imho fits best because this construct cannot have any other meaning (compared to while). But some other people prefer while(1) for the "same" reason...
尽管没有其他帖子中提到的显着差异,但使用
for (;;)
而不是while (1)
的一个常见原因是静态分析工具(以及一些具有某些警告级别的编译器)经常抱怨 while 循环。Goto 有点令人讨厌,但应该生成与其他代码相同的代码。就我个人而言,我坚持使用
for (;;)
(让 Lint 满意),但我对while (1)
没有任何问题。Although there's no significant difference as mentioned in the other posts, a common reason to use
for (;;)
instead ofwhile (1)
is that static analysis tools (and some compilers with certain warning levels) often complain about the while loop.Goto is a bit nasty, but should produce the same code as the others. Personally, I stick to
for (;;)
(to keep Lint happy), but I have no problem withwhile (1)
.while(1)
和for(;;)
完全相同,并且都是易于理解的无限循环代码习惯用法。我会避免使用
goto
:要从无限循环中中断或继续下一次迭代,请使用break
和continue
。while(1)
andfor(;;)
are exactly equivalent and both are well-understood idioms to code infinite loops.I would avoid the use of
goto
: to break from an infinite loop or to proceed to the next iteration, usebreak
andcontinue
.没有任何。使用对您来说最易读的内容
None. Use what is the most readable to you
在 C 中,
true
实现如下(取决于编译器)或
AND false 实现如下
while (1)
相当于while (true) 因为 0 被认为是 false。
while (1) == for (; ;)
因为没有停止条件。它被翻译为汇编程序,因此
如果汇编程序代码没有
ret
或exit
指令,则被视为无限循环。In C,
true
is implemented as follows (depending on compiler)or
AND false is implemented as
so
while (1)
is equivalent towhile (true)
since 0 is considered false.the
while (1) == for (; ;)
as there are no stopping condition.which is translated to assembler as
so if the assembler code doesn't have a
ret
orexit
instruction, it's considered a infinite loop.从我对我的“反汇编岁月”的回忆来看,这不会产生太大的影响(编译器聪明足够)。在我看来,这更多的是关于美学。
From what I recall of my "disassembling years", it won't make much a difference (compilers are smart enough). It is more about aesthetics IMO.