数组指针数组的析构函数

时间:2022-07-10 21:17:28

Ok, I have this constructor:

好的,我有这个构造函数:

Board::Board(int nRows, int nCols){
    numRows=nRows;
    numCols=nCols;

    int** board=new int*[numRows];
    for(int i=0; i<numRows; i++){
        board[i]=new int[numCols];

        for(int j=0; j<numCols; j++){
            board[i] [j]=-1;
        }
    }
}

where board is an array of the number of rows where each item in the array points to an array of length of the number of columns, so this board is set up and initialized to values of -1. My question is how I'm supposed to implement my destructor for a case like this, I understand the concept of the creation of each array of pointers, but in destruction I'm still a little lost. Any help is appreciated!

其中board是行数的数组,其中数组中的每个项指向一个列数的长度数组,因此该板设置并初始化为-1的值。我的问题是我应该如何为这样的情况实现我的析构函数,我理解每个指针数组的创建概念,但在破坏中我仍然有点迷失。任何帮助表示赞赏!

3 个解决方案

#1


4  

First point: don't do it. Just don't. Use an std::vector to store the data, and user operator overloading to allow the client to use 2D dimensions to index into it.

第一点:不要这样做。只是不要。使用std :: vector存储数据,并使用用户操作符重载以允许客户端使用2D维度为其编制索引。

That said, you'll probably insist on doing it anyway, so you might as well do it as well at least know the basic idea: take your original news, and invert them so to speak. So, you started with:

也就是说,无论如何你可能会坚持这样做,所以你也可以这样做,至少知道基本的想法:把原始的新闻,然后反过来说。所以,你开始:

int** board=new int*[numRows];
for(int i=0; i<numRows; i++){
    board[i]=new int[numCols];

Inverting that, you start by deleting the individual items:

反过来说,首先删除单个项目:

for (int i=0; i<numRows; i++)
    delete [] board[i];

Then you delete the outer pointer:

然后删除外部指针:

delete [] board;

I'll repeat though: it's much cleaner to just use a std::vector for the storage.

我会重复一遍:只需使用std :: vector进行存储就可以了。

#2


2  

For everything you allocated with new[], call a delete[], and for everything you allocate with new, call delete.

对于使用new []分配的所有内容,请调用delete [],对于使用new分配的所有内容,请调用delete。

You're doing one top-level new[] allocation (so one delete[] for that) and another numRows new[] allocation, each requiring their own delete[].

你正在做一个*的new []分配(所以一个删除[])和另一个numRows new []分配,每个都需要自己的delete []。

The order should be reversed for deallocation.

为了解除分配,应该撤销该命令。

The actual code is left as an exercise.

实际代码留作练习。

#3


1  

You are better of using std::vector<std::vector<int> >;

你最好使用std :: vector >;

But here's what you need

但这就是你需要的

for(int i=0; i<numRows; i++){
        delete[] board[i];    //Delete each row allotted inside the for loop
    }
 delete[] board;         //Delete the row/array of pointers

#1


4  

First point: don't do it. Just don't. Use an std::vector to store the data, and user operator overloading to allow the client to use 2D dimensions to index into it.

第一点:不要这样做。只是不要。使用std :: vector存储数据,并使用用户操作符重载以允许客户端使用2D维度为其编制索引。

That said, you'll probably insist on doing it anyway, so you might as well do it as well at least know the basic idea: take your original news, and invert them so to speak. So, you started with:

也就是说,无论如何你可能会坚持这样做,所以你也可以这样做,至少知道基本的想法:把原始的新闻,然后反过来说。所以,你开始:

int** board=new int*[numRows];
for(int i=0; i<numRows; i++){
    board[i]=new int[numCols];

Inverting that, you start by deleting the individual items:

反过来说,首先删除单个项目:

for (int i=0; i<numRows; i++)
    delete [] board[i];

Then you delete the outer pointer:

然后删除外部指针:

delete [] board;

I'll repeat though: it's much cleaner to just use a std::vector for the storage.

我会重复一遍:只需使用std :: vector进行存储就可以了。

#2


2  

For everything you allocated with new[], call a delete[], and for everything you allocate with new, call delete.

对于使用new []分配的所有内容,请调用delete [],对于使用new分配的所有内容,请调用delete。

You're doing one top-level new[] allocation (so one delete[] for that) and another numRows new[] allocation, each requiring their own delete[].

你正在做一个*的new []分配(所以一个删除[])和另一个numRows new []分配,每个都需要自己的delete []。

The order should be reversed for deallocation.

为了解除分配,应该撤销该命令。

The actual code is left as an exercise.

实际代码留作练习。

#3


1  

You are better of using std::vector<std::vector<int> >;

你最好使用std :: vector >;

But here's what you need

但这就是你需要的

for(int i=0; i<numRows; i++){
        delete[] board[i];    //Delete each row allotted inside the for loop
    }
 delete[] board;         //Delete the row/array of pointers