这段代码必须崩溃....但工作正常 [英] This code must have crashed....but works fine

查看:61
本文介绍了这段代码必须崩溃....但工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的代码片段:


#include< iostream>

使用namespace :: std;


class myclass

{

public:

myclass(){

cout<< "构造" <<结束;

}

~myclass(){

cout<< "析构函数" << endl;

}

void func(){

cout<< " FUNC" <<结束;

}

};


int main(无效){

myclass * obj = new myclass();

delete obj;

obj = NULL; // obj =(myclass *)0;

obj-> func(); // ............必须崩溃.....但工作正常

返回0;

}


i在Microsoft编译器12.0,13.0和g ++中尝试了上面的代码。

代码没有崩溃,输出就像

构造函数

析构函数

func


i真的不知道当obj为null时,obj-> func()是有效的而不是

崩溃?????????????


任何人对此有所了解????????? ?


问候,

Harsha。

Consider the below code snippet:

#include <iostream>
using namespace::std;

class myclass
{
public:
myclass() {
cout << "constructor" << endl;
}
~myclass() {
cout << "destructor" << endl;
}
void func() {
cout << "func" << endl;
}
};

int main(void) {
myclass *obj = new myclass();
delete obj;
obj = NULL; // obj = (myclass*)0;
obj->func(); //..............must be a crash..... but works fine
return 0;
}

i tried the above code in Microsoft compiler 12.0, 13.0 and g++.
the code doesnt crash and the output will be like
constructor
destructor
func

i really dont know how when obj is null, obj->func() is valid and not
crashing ?????????????/

anybody has an idea about it ??????????

regards,
Harsha.

推荐答案

hs******@gmail.com schrieb:
hs******@gmail.com schrieb:

考虑以下代码片段:
Consider the below code snippet:



[snip]

[snip]


int main(void){

myclass * obj = new myclass();

delete obj;

obj = NULL; // obj =(myclass *)0;

obj-> func(); // ............必须崩溃.....但工作正常

返回0;

}


i在Microsoft编译器12.0,13.0和g ++中尝试了上面的代码。
int main(void) {
myclass *obj = new myclass();
delete obj;
obj = NULL; // obj = (myclass*)0;
obj->func(); //..............must be a crash..... but works fine
return 0;
}

i tried the above code in Microsoft compiler 12.0, 13.0 and g++.



你从哪里得到那个编译器? MS C ++版本为8.0。

Where did you get that compiler? MS C++ is at version 8.0.


代码没有崩溃,输出就像

构造函数

析构函数

func


i真的不知道当obj为null时,obj-> func()是有效的而不是

崩溃???????????? /
the code doesnt crash and the output will be like
constructor
destructor
func

i really dont know how when obj is null, obj->func() is valid and not
crashing ?????????????/



您的代码通过取消引用空指针来调用未定义的行为。

未定义意味着,编译器可以自由地执行它想要的任何操作:这次可能是
似乎可以正常工作,或者它可能会格式化您的硬盘。


-

Thomas

Your code invokes undefined behaviour by dereferencing a null-pointer.
Undefined means, the compiler is free to do everything it wants: It might
appear to work this time, or it might format your hard disk.

--
Thomas




hshar ... @ gmail.com写道:

hshar...@gmail.com wrote:

考虑以下代码片段:


#include< iostream>

using namespace :: std;


class myclass

{

public:

myclass(){

cout<< "构造" <<结束;

}

~myclass(){

cout<< "析构函数" << endl;

}

void func(){

cout<< " FUNC" <<结束;

}

};


int main(无效){

myclass * obj = new myclass();

delete obj;

obj = NULL; // obj =(myclass *)0;

obj-> func(); // ............必须崩溃.....但工作正常
Consider the below code snippet:

#include <iostream>
using namespace::std;

class myclass
{
public:
myclass() {
cout << "constructor" << endl;
}
~myclass() {
cout << "destructor" << endl;
}
void func() {
cout << "func" << endl;
}
};

int main(void) {
myclass *obj = new myclass();
delete obj;
obj = NULL; // obj = (myclass*)0;
obj->func(); //..............must be a crash..... but works fine



实际上,这只会导致未定义的行为。

Actually, that merely results in undefined behavior.


返回0;

}


i尝试使用Microsoft编译器中的上述代码12.0,13.0和g ++。

代码没有崩溃,输出就像是

构造函数

析构函数

func


i真的不知道当obj为null时,obj-> func()是有效的而不是

崩溃???????? ????? /


任何人对此有所了解??????????
return 0;
}

i tried the above code in Microsoft compiler 12.0, 13.0 and g++.
the code doesnt crash and the output will be like
constructor
destructor
func

i really dont know how when obj is null, obj->func() is valid and not
crashing ?????????????/

anybody has an idea about it ??????????



当你有未定义的行为时,这意味着任何事情都可能发生。

例如(1)程序可能崩溃,(2)如果你有一个有效的对象(由于你的类没有成员和

因此空指针永远不会被取消引用),或者(3)程序

可能打印出来Harsha在他的

帖子中使用了太多问号(''''')"在你的控制台上一遍又一遍。如果它未定义,你就不知道了。
只是不知道。


祝你好运,


Tom

When you have undefined behavior, that means anything could happen.
For example (1) the program might crash, (2) the program might work as
if you had a valid object (since your class has no members and
therefore the null pointer is never dereferenced), or (3) the program
might print "Harsha uses far too many question marks (''?'') in his
postings" on your console over and over again. If it''s undefined, you
just don''t know.

Best regards,

Tom


hs ****** @ gmail。 com 写道:
hs******@gmail.com wrote:

请考虑以下代码段:


#include< iostream>

使用namespace :: std;


class myclass

{

public:

myclass(){

cout<< "构造" <<结束;

}

~myclass(){

cout<< "析构函数" << endl;

}

void func(){

cout<< " FUNC" <<结束;

}

};


int main(无效){

myclass * obj = new myclass();

delete obj;

obj = NULL; // obj =(myclass *)0;

obj-> func(); // ............必须崩溃.....但工作正常
Consider the below code snippet:

#include <iostream>
using namespace::std;

class myclass
{
public:
myclass() {
cout << "constructor" << endl;
}
~myclass() {
cout << "destructor" << endl;
}
void func() {
cout << "func" << endl;
}
};

int main(void) {
myclass *obj = new myclass();
delete obj;
obj = NULL; // obj = (myclass*)0;
obj->func(); //..............must be a crash..... but works fine



C ++标准没有'' t要求任何代码''崩溃''。

The C++ standard doesn''t require any code to ''crash''.


返回0;

}


i在Microsoft编译器12.0,13.0和g ++中尝试了上面的代码。

代码没有崩溃,输出就像

构造函数

析构函数

func


i真的不知道当obj为null时,obj-> func()是有效的而不是

崩溃??????????? /
return 0;
}

i tried the above code in Microsoft compiler 12.0, 13.0 and g++.
the code doesnt crash and the output will be like
constructor
destructor
func

i really dont know how when obj is null, obj->func() is valid and not
crashing ?????????????/



是什么让你觉得没有其他可能的结果而不是崩溃? />

What makes you think there is no other possible outcome than a ''crash''?


任何人对此有所了解??????????
anybody has an idea about it ??????????



尝试修复''?''键。它好像坏了。

Try to repair your ''?'' key. It seems broken.


这篇关于这段代码必须崩溃....但工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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