我在c ++中有一个动态数组,定义如下:
string** CommunityNodes = new string *[10000];
for (int i = 0; i < 10000; ++i) {
CommunityNodes[i] = new string[1000];
}
如果数组类型是int类型,我可以像这样释放它:
int ** array_iter = CommunityNodes;
while(*array_iter){
delete(*array_iter++);
}
delete CommunityNodes;
但我的数组类型是字符串,通过上面的方式,我得到一些错误。那么如何释放这个动态数组的共存内存呢?
答案 0 :(得分:4)
如果使用new []
分配内存,则需要使用delete []
:
for (int i = 0; i < 10000; ++i) {
delete [] CommunityNodes[i];
}
delete [] CommunityNodes;