【发布时间】:2017-12-25 21:45:35
【问题描述】:
我写了一个非常简单的代码。在动态创建一个对象,然后我删除该对象并将其分配为零。之后我访问该对象的成员函数,但我的程序没有崩溃,而是返回值。
class MyClass
{
public:
MyClass() {}
int funct() { return 0; }
};
int main()
{
MyClass *mc = new MyClass;
delete mc;
mc = 0;
// The program should crash here as I've set mc to zero after deleting
// it. But it returns the value.
int retVal = mc->funct();
return 0;
}
根据我对 new、delete 和赋值为零的理解,此代码应该会崩溃或给出异常。
【问题讨论】:
-
未定义的行为不会导致崩溃。
标签: c++