问题描述
Hi Buddies,
我已经阅读了很多关于不使用GOTO语句选择的东西
a良好的编程风格[Ref 。
任何人都可以给出一些显示有害的特定代码行
的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...
推荐答案
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.
我在移动开发中使用它来处理错误清理。
通常情况下我会使用
例外情况,但这对移动电话来说要花费很多开销。这是一个例子:
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 ;)
哦...我看到你请求了一个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的具体例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!