【发布时间】:2014-12-10 00:35:39
【问题描述】:
所以,如果 if 有一个指针数组
stuff **items;
stuff *testPTR;
items = new stuff[200];
其中的内容用于保存动态内存对象
items[0] = new stuff;
items[1] = new stuff;
etc.
testPTR = items[0];
我想删除项目,怎么不行?
delete items[0];
delete items[1];
etc.
delete [] items;
我测试了这段代码,我仍然可以访问使用我之前设置的 testPTR(在删除语句运行之后)分配的内容。
//stuff held in items[0] is still accessible with
*testPTR
我很困惑,因为如果我 cout items[any position] 我会得到一个内存地址,delete 应该使用它来返回分配给东西的内存,但上面的代码似乎留下了东西,只删除了指针数组.我错过了什么? 谢谢 //编辑以使问题更清晰
【问题讨论】:
-
你为什么不用
std::vector<std::unique_ptr<stuff>>然后你就不用再删除任何东西了。