简体   繁体   English

删除指针数组的正确方法

[英]Proper way to delete an array of pointers

I have an array of pointers (that I created by calling new ptr*[size] ). 我有一个指针数组(我通过调用new ptr*[size] )。 All of these pointers point to an object that was also put on the heap. 所有这些指针都指向一个也放在堆上的对象。

What is the proper way to delete the array and all new'd ptr's? 删除数组和所有新的ptr的正确方法是什么?

This is what I do now: 这就是我现在所做的:

for (int i = 0; i < size; i++) delete array[i];
delete[] array; // Not sure since this double deletes array[0]

Does this do what I think it should? 这是否符合我的想法?

Thanks 谢谢

Every pointer allocated with new gets a corresponding delete . 分配有new每个指针都会获得相应的delete Every pointer allocated with new [] gets a corresponding delete [] . new []分配的每个指针都得到一个相应的delete [] That's really all you need to know. 这真的是你需要知道的。 Of course, when you have a dynamically allocated array which contains dynamically allocated pointers the deallocation must occur in reverse order. 当然,当你有一个包含动态分配指针的动态分配数组时,释放必须以相反的顺序发生。

So it follows that the correct idiom would be... 因此,正确的成语将是......

int main()
{
    int **container = new int*[n];
    for(int i = 0; i < n; ++i)
        container[i] = new int[size];

    // ... and to deallocate...
    for(int i = 0; i < n; ++i)
        delete [] container[i];

    delete [] container;
}

And then of course I say "stop doing that" and recommend you use a std::array or std::vector (and the template type would be unique_ptr<int> ). 然后当然我说“停止这样做”并建议你使用std::arraystd::vector (模板类型将是unique_ptr<int> )。

Yes, that does what you think it should. 是的,那就是您认为应该做的。 Since you did new for each element, you have to delete each element. 由于您为每个元素执行了new ,因此必须delete每个元素。 And since you did new[] for the entire array, you need to delete[] the entire array. 既然你为整个数组做了new[] ,你需要delete[]整个数组。

As @djechlin rightly says in the comments, there's not really enough information to go on, but I'm presuming your prior code is something like this: 正如@djechlin在评论中正确地说的那样,还没有足够的信息继续下去,但我假设您之前的代码是这样的:

int** array = new int*[5];
for (int i = 0; i < 5; i++) {
  array[i] = new int;
}

Note that array is not actually an array type. 请注意,数组实际上不是数组类型。 It is a "pointer to pointer to int" and the array of pointers it points to was allocated with new[] . 它是“指向int的指针”,它指向的指针数组是用new[]分配的。 That's why you need to delete[] it. 这就是你需要delete[]它的原因。

Yes. 是。 First you have to free the object each pointer in the array points to, then you have to free the array itself. 首先,你必须释放数组中每个指针指向的对象,然后你必须释放数组本身。 In that order. 以该顺序。 If you reverse the order you'll have no reference to the objects and will leak a lot of memory. 如果颠倒顺序,则不会引用对象,并会泄漏大量内存。

Yes, first you delete each object to which elements of array point, and then you delete array of pointers itself. 是的,首先删除数组元素指向的每个对象,然后删除指针数组本身。 If you want to check your memory management, you can use tools like valgrind , they will be able to spot most errors. 如果你想检查你的内存管理,你可以使用像valgrind这样的工具,他们将能够发现大多数错误。

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

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