使用GOTO? [英] Use GOTO or not?

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

问题描述

目前,我正在一个项目,其中goto语句被大胆使用。 goto语句的主要目的是在例程中有一个清除节,而不是多个返回语句。
如下:

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;
}

这使得它更容易,因为我们可以跟踪我们的清理代码在一个部分

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 ++中有一个名为 resource acquisition is initialization 的概念,你的析构函数应该负责清理这些东西。

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 :让我清楚一点。

考虑下面的代码: p>

Consider the following code:

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?

解决方案:使用对象引用,

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.

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

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