关于我们为什么不使用GOTO的具体例子 [英] Particular Example about why should we not use GOTO

查看:95
本文介绍了关于我们为什么不使用GOTO的具体例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Buddies,


我已经阅读了很多关于不使用GOTO语句选择的东西

a良好的编程风格[Ref http://david.tribble.com/text/goto.html]

任何人都可以给出一些显示有害的特定代码行

的GOTO。


SoftEast ......

Hi Buddies,

I have read a lot of stuffs regarding not using GOTO statements to opt
a good programming style [Ref http://david.tribble.com/text/goto.html].
Can anybody give a particular lines of code which shows harmfullness
of GOTO.

SoftEast...

推荐答案

29 Mrz。,09:54,SoftEast < hemendra.sw ... @ gmail.comwrote:
On 29 Mrz., 09:54, "SoftEast" <hemendra.sw...@gmail.comwrote:

Hi Buddies,


我已经阅读了很多关于不使用GOTO语句选择的东西

a良好的编程风格[Refhttp://david.tribble.com/text/goto.html]。

任何人都可以给出一个特定的显示有害的代码行
GOTO的



SoftEast ...
Hi Buddies,

I have read a lot of stuffs regarding not using GOTO statements to opt
a good programming style [Refhttp://david.tribble.com/text/goto.html].
Can anybody give a particular lines of code which shows harmfullness
of GOTO.

SoftEast...



I在移动开发中使用它来进行错误处理和清理。

通常情况下我会使用

例外,但这对移动设备来说会有很多开销

手机。这是一个例子:


bool DoSomething()//失败时返回false

{

SomeObject * obj1 = NULL, obj2 = NULL;


obj2 = new SomeObject();

if(!ThisMayFail(obj1))

goto failed;


obj2 = new SomeObject();

if(!ThisMayFail(obj2))

goto失败;


返回true;


失败:

如果(obj1!= NULL)

删除obj1 ;

if(obj2!= NULL)

删除obj2;

返回false;

}


我认为这种清理技术很容易理解并且

维持。

I use it in Mobile Development for error handling and clean up.
Normally I would use
exceptions for that, but this would be to much overhead for mobile
phones. Here is an example:

bool DoSomething() //returns false on failure
{
SomeObject* obj1 = NULL, obj2 = NULL;

obj2 = new SomeObject();
if(!ThisMayFail(obj1))
goto failed;

obj2 = new SomeObject();
if(!ThisMayFail(obj2))
goto failed;

return true;

failed:
if(obj1 != NULL)
delete obj1;
if(obj2 != NULL)
delete obj2;
return false;
}

I think this kind of clean up technique is easy to understand and
maintain.


29岁的Mrz。,14:19,Alexander Block < abloc ... @ googlemail.comwrote:
On 29 Mrz., 14:19, "Alexander Block" <abloc...@googlemail.comwrote:

On 29 Mrz。,09:54," SoftEast" < hemendra.sw ... @ gmail.comwrote:
On 29 Mrz., 09:54, "SoftEast" <hemendra.sw...@gmail.comwrote:

Hi Buddies,
Hi Buddies,


我已经阅读了很多关于不使用GOTO语句来选择

a良好的编程风格的东西[参见://david.tribble.com/text/goto.html]。

任何人都可以提供特定的代码行,这些代码显示有害的GOTO

I have read a lot of stuffs regarding not using GOTO statements to opt
a good programming style [Refhttp://david.tribble.com/text/goto.html].
Can anybody give a particular lines of code which shows harmfullness
of GOTO.


SoftEast ...
SoftEast...



我在移动开发中使用它来处理错误清理。

通常情况下我会使用

例外情况,但这对移动电话来说要花费很多开销。这是一个例子:


bool DoSomething()//失败时返回false

{

SomeObject * obj1 = NULL, obj2 = NULL;


obj2 = new SomeObject();

if(!ThisMayFail(obj1))

goto failed;


obj2 = new SomeObject();

if(!ThisMayFail(obj2))

goto失败;


返回true;


失败:

如果(obj1!= NULL)

删除obj1 ;

if(obj2!= NULL)

删除obj2;

返回false;


}


我认为这种清理技术很容易理解,而且b $ b维持。


I use it in Mobile Development for error handling and clean up.
Normally I would use
exceptions for that, but this would be to much overhead for mobile
phones. Here is an example:

bool DoSomething() //returns false on failure
{
SomeObject* obj1 = NULL, obj2 = NULL;

obj2 = new SomeObject();
if(!ThisMayFail(obj1))
goto failed;

obj2 = new SomeObject();
if(!ThisMayFail(obj2))
goto failed;

return true;

failed:
if(obj1 != NULL)
delete obj1;
if(obj2 != NULL)
delete obj2;
return false;

}

I think this kind of clean up technique is easy to understand and
maintain.



哦......我看到你请求了一个HARMFULL而不是USEFULL的例子:D

如果你想要它有害,请删除NULL初始化在方法的第一个

行中;)

Oh...I see you requested a HARMFULL and not USEFULL example :D
If you want it harmfull, remove the NULL initialization in the first
line of the method ;)


29 Mrz。,14:22,Alexander Block < abloc ... @ googlemail.comwrote:
On 29 Mrz., 14:22, "Alexander Block" <abloc...@googlemail.comwrote:

29 Mrz。,14:19,Alexander Block < abloc ... @ googlemail.comwrote:
On 29 Mrz., 14:19, "Alexander Block" <abloc...@googlemail.comwrote:

On 29 Mrz。,09:54," SoftEast" < hemendra.sw ... @ gmail.comwrote:
On 29 Mrz., 09:54, "SoftEast" <hemendra.sw...@gmail.comwrote:


Hi Buddies,
Hi Buddies,


我已经阅读了很多关于不使用GOTO语句来选择的东西

a good programming style [Refhttp://david.tribble.com/text/goto.html]。

任何人都可以提供特定的代码行,显示有害的

的GOTO。
I have read a lot of stuffs regarding not using GOTO statements to opt
a good programming style [Refhttp://david.tribble.com/text/goto.html].
Can anybody give a particular lines of code which shows harmfullness
of GOTO.


SoftEast ...
SoftEast...


我在移动开发中使用它来进行错误处理和清理。

通常我会使用

例外,但这会移动电话需要很多开销

手机。下面是一个例子:
I use it in Mobile Development for error handling and clean up.
Normally I would use
exceptions for that, but this would be to much overhead for mobile
phones. Here is an example:


bool DoSomething()//失败时返回false

{

SomeObject * obj1 = NULL,obj2 = NULL;
bool DoSomething() //returns false on failure
{
SomeObject* obj1 = NULL, obj2 = NULL;


obj2 = new SomeObject();

if(!ThisMayFail(obj1))

转到失败;
obj2 = new SomeObject();
if(!ThisMayFail(obj1))
goto failed;


obj2 = new SomeObject();

if(!ThisMayFail(obj2))

转到失败;
obj2 = new SomeObject();
if(!ThisMayFail(obj2))
goto failed;


返回true;
return true;


失败:

if(obj1!= NULL)

删除obj1;

if(obj2!= NULL)

删除obj2;

返回false;
failed:
if(obj1 != NULL)
delete obj1;
if(obj2 != NULL)
delete obj2;
return false;


}
}


我认为这种清理技术是易于理解和

维持。
I think this kind of clean up technique is easy to understand and
maintain.



哦...我看到你请求了一个HARMFULL而不是USEFULL示例:D

如果你想要它有害,请删除NULL初始化在该方法的第一个

行;)


Oh...I see you requested a HARMFULL and not USEFULL example :D
If you want it harmfull, remove the NULL initialization in the first
line of the method ;)



一个更好的例子来表示有害物质:


class CTest

{

public:

char * str;


CTest()

{

str ="正确初始化';

}


void UseMe()

{

OutputDebugStringA(str);

}

};


int main()

{

CTest test1;


goto harml;


CTest test2;


harml:

test1.UseMe();

test2.UseMe();


返回0;

}


goto将跳过CTest的构造函数。编译器不能检查那个,所以你有一个看起来不错的对象,但是没有初始化的
。在大多数情况下,上面的示例将在

test2.UseMe()上崩溃,因为test2.str具有未初始化的值。

A better example for harmfull gotos:

class CTest
{
public:
char* str;

CTest()
{
str = "Initialized correctly";
}

void UseMe()
{
OutputDebugStringA(str);
}
};

int main()
{
CTest test1;

goto harmfull;

CTest test2;

harmfull:
test1.UseMe();
test2.UseMe();

return 0;
}

The goto will skip the constructor of CTest here. The compiler is not
able to check that, so you have an object that looks ok, but is not
initialized. In most cases, the above example will crash on
test2.UseMe(), because test2.str has an uninitialized value.


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

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