为什么不删除销毁任何东西? [英] Why doesn't delete destroy anything?

查看:110
本文介绍了为什么不删除销毁任何东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩一些内存动态分配,但我没有得到一点。当使用 new 语句分配一些内存时,我应该能够破坏指针指向的内存使用 delete

I'm playing a little with memory dynamic allocation, but I don't get a point. When allocating some memory with the new statement, I'm supposed to be able to destroy the memory the pointer points to using delete.

但是当我尝试,这个 delete 命令似乎不工作,因为空间的指针

But when I try, this delete command doesn't seem to work since the space the pointer is pointing at doesn't seem to have been emptied.

让我们以这个真正基本的代码为例:

Let's take this truly basic piece of code as an example:

#include <iostream>  

using namespace std;

int main()  
{  
    //I create a pointer-to-integer pTest, make it point to some new space,  
    // and fulfill this free space with a number;  
    int* pTest;  
    pTest = new int;  
    *(pTest) = 3;  
    cout << *(pTest) << endl; 

    // things are working well so far. Let's destroy this
    // dynamically allocated space!
    delete pTest;

    //OK, now I guess the data pTest pointed to has been destroyed 
    cout << *(pTest) << endl; // Oh... Well, I was mistaking.  

    return 0;  
}  

任何线索?

推荐答案

现在是时候学习什么是未定义的行为。 :)

It's time to learn what undefined behavior is. :)

在C ++中,当您执行非法/无意义/错误/等等时。标准经常说它导致未定义的行为。这意味着从那时起,你的程序的状态是完全无保证的,任何事情都可能发生。

In C++, when you do something illegal/nonsensical/bad/etc. the standard often says that "it leads to undefined behavior." This means that from that point forward, the state of your program is completely non-guaranteed, and anything could happen.

在你最后一个 *(pTest),你会得到未定义的行为。这是因为 pTest 不指向有效的对象,并且取消引用这样的指针是未定义的。所以你看到的是完全允许的:未定义的输出。

At the point where you do your last *(pTest), you get undefined behavior. This is because pTest does not point to a valid object, and dereferencing such a pointer is undefined. So what you're seeing is totally allowed: undefined output.

你所做的都是我完成了这个分配。一旦你说过,你不应该(甚至不能)再检查或关心那个记忆。它甚至没有概念意义解除分配的东西,然后尝试使用它;你已经说完了!

All you've done is said "I'm finished with this allocation." Once you've said that, you shouldn't (and indeed, cannot) inspect or care about that memory any longer. It doesn't even make conceptual sense to deallocate something then try to use it; you've said you were done!

你的输出是有点可预测的:可能,你的操作系统只是说好吧,感谢内存,就是这样。它没有理由实际重置内存,或做任何特别的。这真的是浪费时间,当没有人(包括你自己的程序)不使用它。

Your output is somewhat predictable though: likely, your OS simply says "okay, thanks for the memory" and that's it. It has no reason to actually "reset" the memory, or do anything special. That would indeed be a waste of time, when nobody (including your own program) is not using it.

但是记住,这个输出是完全未定义的。不要尝试使用不存在的对象。也许一个更好的测试是:

But remember, this output is completely undefined. Don't try to use objects that don't exist. Perhaps a better test would have been:

#include <iostream>

struct foo
{
    ~foo()
    {
        std::cout << "foo is gone :(" << std::endl;
    }
};

int main(void)
{
    foo* f = new foo();
    delete f; // you'll see that the object is destroyed.
}

虽然看起来你正在查看内存本身发生了什么,只要记住删除内存,然后尝试使用它是没有意义的,所以答案是:who知道,这取决于你的特定平台,C ++不在乎。

Although it seems you were looking to see what happens with the memory itself. Just remember that it makes no sense to get rid of memory then try to use it, so the answer is: who knows. It's up to your specific platform, which C++ doesn't care about.

这篇关于为什么不删除销毁任何东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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