而(1)..打破,而不是转到 [英] while(1) .. break instead of goto

查看:135
本文介绍了而(1)..打破,而不是转到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现下面的code在C程序中:

I found the following code in a C program:

while (1)
{
    do_something();
    if (was_an_error()) break;

     do_something_else();
     if (was_an_error()) break;

     [...]

     break;
}
[cleanup code]

下面而(1)被用作终于本地仿真。你也可以这样写使用转到取值:

Here while(1) is used as local emulation of "finally". You can also write this using gotos:

do_something()
if (was_an_error()) goto out;

do_something_else()
if (was_an_error()) goto out;

[...]
out:
[cleanup code]

我以为转到解决方案是一个常用的成语。我已经看到了这个成语的几个出现在内核源代码,它也是在季奥米季斯Spinellis'code阅读书中提到的。

I thought the goto solution is a usual idiom. I have seen several occurrences of this idiom in the kernel sources and it is also mentioned in Diomidis Spinellis' "Code Reading" book.

我的问题是:什么解决方案是更好?有没有使用而(1)解决任何具体的原因是什么?

My question is: What solution is better? Is there any specific reason to use the while(1) solution?

问题<一href=\"http://stackoverflow.com/questions/943826/how-to-simulate-exceptions-in-c-with-goto\">943826不回答我的问题。

推荐答案

看似普遍revultion到GOTO很大程度上是由于Edsger Dijkstra的信转到语句是有害的。

The seemingly universal revultion to GOTO is largely due to Edsger Dijkstra's letter "Go To Statement Considered Harmful".

如果你坚决不使用goto语句,像

If you are determined not to use goto, something like

do {
    ...
while(0);

大概比,而安全(1){...},因为它保证你不会在不经意间循环(如果你无意中循环,有一段时间(1)你可能无意中无限循环)。

is probably safer than while(1) { ... } since it guarantees that you will not inadvertently loop (and if you are inadvertently looping, with while(1) you are probably inadvertently looping infinitely).

这(AB)用做的一个好处/休息/ while或同时/为此打破了超过转到的是,你保证不被跳跃的上方的构造 - 转到可以用于同样的功能内较早跳到一个标签

The one advantage that (ab)using do/break/while or while/break for this purpose has over goto is that you are guaranteed not to be jumping above the construct -- goto can be used to jump to a label earlier within the same function.

这是做/休息/等,同时有超过转到它的缺点是,你是限于一个退出点(循环后)。在某些情况下,你可能需要一个清理上演:例如,当你打开一个文件句柄,malloc的一些内存,从文件中读取...如果读取失败时,需要清理的malloc。如果malloc的失败,你不需要把它清理干净,但你仍然需要清理的文件句柄。随着跳转,你可以根据你发生错误的位置有每清理阶段的一个标签,并跳转到precisely右键点。

The disadvantage that do/break/while etc. have over goto is that you are limited to one exit point (immediately after the loop). In some cases you might need a staged cleanup: e.g., when you open a file handle, malloc some memory, read from the file... if the read fails, you need to clean up the malloc. If the malloc fails, you don't need to clean it up, but you still need to clean up the file handle. With goto, you can have one label per stage of cleanup and jump to precisely the right point depending on where your error occurred.

在我看来一味的回避,因为prevalent仇恨GOTO它比仔细推理出的情况下其对案件逐案基础上使用更具破坏性。我使用的经验法则是不Linux内核做呢?如果是的话,它不能的的坏。替代品的Linux内核与现代软件工程的其他任何很好的例子。

In my opinion blindly avoiding GOTO because of the prevalent hatred of it is more damaging than carefully reasoning out a case for its use on a case-by-case basis. A rule of thumb I use is "does the Linux kernel do it? If so, it can't be that bad". Substitute linux kernel with any other good example of modern software engineering.

这篇关于而(1)..打破,而不是转到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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