我正在玩一些内存动态分配,但我不明白。当使用new
语句分配一些内存时,我应该能够破坏指针指向的内存 using delete
。
但是当我尝试时,这个delete
命令似乎不起作用,因为指针指向的空间似乎没有被清空。
让我们以这段真正基本的代码为例:
#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;
}
有什么线索吗?