我玩的内存动态分配一点,但我没有得到一个观点。当用new
语句分配一些内存时,我应该能够销毁指针指向使用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;
}
任何线索?
是的,一个小菜一碟,但不是一个小事。 – sharptooth 2010-07-19 11:24:38