去还是不去? [英] To GOTO or not to GOTO?

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

问题描述

当前,我正在一个项目中大量使用goto语句。 goto语句的主要目的是在例程中具有一个清理段,而不是多个return语句。
如下所示:

Currently I am working on a project where goto statements are heavely used. The main purpose of goto statements is to have one cleanup section in a routine rather than multiple return statements. Like below:

BOOL foo()
{
   BOOL bRetVal = FALSE;
   int *p = NULL;

   p = new int;
   if (p == NULL)
   {
     cout<<" OOM \n";
     goto Exit;
   }

   // Lot of code...

Exit:
   if(p)
   {
     delete p;
     p = NULL;
   }
   return bRetVal;
}

这使我们可以更轻松地跟踪某一段的清理代码在代码中,即在Exit标签之后。

This makes it much easier as we can track our clean up code at one section in code, that is, after the Exit label.

但是,我读过很多地方,使用goto语句是不好的做法。

However, I have read many places it's bad practice to have goto statements.

目前,我正在阅读 代码完整 ,它表示我们需要在声明附近使用变量。如果使用goto,则需要在首次使用goto之前声明/初始化所有变量,否则编译器将给出错误,表明goto语句会跳过xx变量的初始化。

Currently I am reading the Code Complete book, and it says that we need to use variables close to their declarations. If we use goto then we need to declare/initialize all variables before first use of goto otherwise the compiler will give errors that initialization of xx variable is skipped by the goto statement.

哪种方法是正确的?

根据Scott的评论:

From Scott's comment:

使用goto从一个部分跳转到另一部分似乎很糟糕,因为它会使代码难以阅读和理解。

It looks like using goto to jump from one section to another is bad as it makes the code hard to read and understand.

但是如果我们使用goto只是向前移动并贴上一个标签,应该就可以了(?)。

But if we use goto just to go forward and to one label then it should be fine(?).

推荐答案

我不确定清理代码是什么意思但是在C ++中,有一个名为 资源获取是初始化的概念,应该由析构函数负责清理内容。

I am not sure what do you mean by clean up code but in C++ there is a concept called "resource acquisition is initialization" and it should be the responsibility of your destructors to clean up stuff.

(请注意,在C#和Java中,通常可以通过try / finally解决此问题)

(Note that in C# and Java, this is usually solved by try/finally)

有关更多信息,请访问此页面:
http://www.research.att.com/~bs/bs_faq2.html#finally

For more info check out this page: http://www.research.att.com/~bs/bs_faq2.html#finally

编辑 :让我稍微澄清一下。

EDIT: Let me clear this up a little bit.

考虑以下代码:

void MyMethod()
{
    MyClass *myInstance = new MyClass("myParameter");
    /* Your code here */
    delete myInstance;
}

问题:如果您有多个从功能退出?您必须跟踪每个出口并在所有可能的出口处删除对象!否则,您将出现内存泄漏和僵尸资源,对吧?

The problem: What happens if you have multiple exits from the function? You have to keep track of each exit and delete your objects at all possible exits! Otherwise, you will have memory leaks and zombie resources, right?

解决方案:使用对象引用代替,因为它们会在清除时自动清理

The solution: Use object references instead, as they get cleaned up automatically when the control leaves the scope.

void MyMethod()
{
    MyClass myInstance("myParameter");
    /* Your code here */
    /* You don't need delete - myInstance will be destructed and deleted
     * automatically on function exit */
}

哦,是的,并使用 std :: unique_ptr 或类似的东西,因为上面的示例显然是不完善的。

Oh yes, and use std::unique_ptr or something similar because the example above as it is is obviously imperfect.

这篇关于去还是不去?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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