为什么删除不破坏任何东西?
我在玩一点内存动态分配,但我没有明白.当使用 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.
}
尽管您似乎想看看内存本身会发生什么.请记住,摆脱内存然后尝试使用它是没有意义的,所以答案是:谁知道.这取决于您的特定平台,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.
相关文章