简体   繁体   English

如何在C ++中释放动态数组的内存

[英]how release memory of a dynamic array in c++

I have a dynamic array in c++ that defined like this: 我在c ++中有一个动态数组,其定义如下:

string** CommunityNodes = new string *[10000];
for (int i = 0; i < 10000; ++i) {
     CommunityNodes[i] = new string[1000];
}

If the array type would be in int type, i can release it like this: 如果数组类型为int类型,我可以这样释放它:

int ** array_iter = CommunityNodes;
    while(*array_iter){
            delete(*array_iter++);
}
delete CommunityNodes;

but my array type is string and by the above way, i get some errors. 但是我的数组类型是字符串,通过上述方式,我遇到了一些错误。 so how can I release the alocated memory of this dynamic array? 那么如何释放该动态数组的分配内存?

If you allocate memory using new [] , you need to use delete [] : 如果使用new []分配内存,则需要使用delete []

for (int i = 0; i < 10000; ++i) {
    delete [] CommunityNodes[i];
}
delete [] CommunityNodes;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM