删除指针 [英] delete pointer

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

问题描述



为什么我仍然可以使用这个对象,即使我删除了它?


我得到的输出是:

Cat构造

Cat解构//删除对象

Cat Eats。 //我的问题 - 为什么对象仍然有效?


谢谢

yin99


----- BEGIN CODE SAMPLE ---------------

#include< iostream>

使用命名空间std;

class Cat

{

public:

void sleep(){cout<< \ n猫睡觉! \ n; }

speak(){cout<< \ n MEOW! \ n; }

Cat(){cout<< \ n Cat Constructs \ n ; }

~Cat(){cout<< \ n Cat De-structs \ n ; }

void eat(){cout<< \ n Cat Eats.\ ;}

私人:

};


int main()

{

猫*珍珠=新猫();

删除珍珠;

pearl-> eat(); //作品!

返回0;

}

解决方案

Yin99写道:

为什么我仍然可以使用这个对象,即使我删除了它?

我得到的输出是:
Cat构造
Cat De-structs //删除对象
Cat Eats。 //我的问题 - 为什么对象仍然有效?
谢谢
yin99

-----开始代码示例--------- ------
#include< iostream>
使用命名空间std;
类Cat
{
public:
void sleep(){ cout<< \ n猫睡觉! \ n; }
speak(){cout<< \ n MEOW! \ n;猫(){cout<< \ n Cat Constructs \ n ; }
~Cat(){cout<< \ n Cat De-structs \ n ; }
void eat(){cout<< \ n Cat Eats.\ ;}
私人:
};

int main()
{* *猫*珍珠=新猫();
删除珍珠;
pearl-> eat(); //工作!
返回0;
}




看到之后没有分配内存,你没有删除

指针,它只是一个有效的对象。


删除不会破坏对象。即使你已经在

中分配了被删除的对象的内存,它仍然可以工作。


请注意,这种行为是不能保证的,不适用于a

更复杂的程序。


另外,你的Cat * pearl = new Cat(); line应为Cat * pearl = new Cat;

如果构造函数有0个参数,则使用No()。


- John Ratliff


Yin99写道:


为什么我仍然可以使用这个对象,即使我删除了它?



您已执行未定义的行为。标准没有指定

会发生什么。一个可能的结果是它似乎首先在

工作。你可能也在捣毁免费商店,稍后会收到一个

无法解释的错误。


这样的话要教会你很少的语言,

并且通常对初学者没用。


Brian


>为什么我仍然可以使用这个对象,即使我已经删除了它?

因为你调用了未定义的行为,这实际上意味着任何事情都可以发生.b / b <


Why am I still able to use this object even though I have deleted it?

The Output I get is:
Cat Constructs
Cat De-structs //deleteing object
Cat Eats. //My question- why does object still work?

Thanks
yin99

----- BEGIN CODE SAMPLE ---------------
#include <iostream>
using namespace std;
class Cat
{
public:
void sleep() { cout << "\n Cat sleeps! \n "; }
speak() { cout << "\n MEOW! \n "; }
Cat() { cout << "\n Cat Constructs \n" ; }
~Cat() { cout << "\n Cat De-structs \n" ; }
void eat() { cout << "\n Cat Eats.\n" ;}
private:
};

int main ()
{
Cat *pearl = new Cat();
delete pearl;
pearl->eat(); //Works!
return 0;
}

解决方案

Yin99 wrote:

Why am I still able to use this object even though I have deleted it?

The Output I get is:
Cat Constructs
Cat De-structs //deleteing object
Cat Eats. //My question- why does object still work?

Thanks
yin99

----- BEGIN CODE SAMPLE ---------------
#include <iostream>
using namespace std;
class Cat
{
public:
void sleep() { cout << "\n Cat sleeps! \n "; }
speak() { cout << "\n MEOW! \n "; }
Cat() { cout << "\n Cat Constructs \n" ; }
~Cat() { cout << "\n Cat De-structs \n" ; }
void eat() { cout << "\n Cat Eats.\n" ;}
private:
};

int main ()
{
Cat *pearl = new Cat();
delete pearl;
pearl->eat(); //Works!
return 0;
}



Seeing as how no memory was allocated after that, and you didn''t erase
the pointer, it was simply still a valid object.

Delete doesn''t destroy the object. Even if you had allocated memory in
your object that was deleted, it might still work.

Note that this behaviour is NOT guaranteed and would NOT work with a
more complicated program.

Also, your Cat *pearl = new Cat(); line should be Cat *pearl = new Cat;
No () are used if there are 0 parameters to the constructor.

--John Ratliff


Yin99 wrote:


Why am I still able to use this object even though I have deleted it?


You have performed Undefined Behavior. The Standard doesn''t specify
what will happen. One possible outcome is that it will seem to work at
first. You may also be trashing the free store and will get an
unexplained error later.

Messing around like this teaches you very little about the language,
and is generally not useful for beginners.


Brian


> Why am I still able to use this object even though I have deleted it?
Because you invoked undefined behavior, which essentially means anything can
happen.


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

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