declare and delete this version of a 2D array in c++

时间:2021-06-29 20:56:46
int *array[10];
for(int i = 0; i < 10; i++)
    array[i] = new int[10];
//...
void passFunc(int *a[10]) //array containing pointers
{
    //...
}

passFunc(array); 

Im trying to figure out how to declare and delete this version of a 2D array. I started using int ** array, but in order to make one section of code easier, I need to switch to *[]. Can anyone help me out? I have tried compiling my actual code (the above code is just an example), which looks like this:

我试图弄清楚如何声明和删除这个版本的2D数组。我开始使用int **数组,但为了使代码的一部分更容易,我需要切换到* []。谁能帮我吗?我试过编译我的实际代码(上面的代码只是一个例子),看起来像这样:

int* filedata[LENGTH] = new int*[LENGTH]; //ERROR: array must be initialized with brace- enclosed identifiers.

int * filedata [LENGTH] = new int * [LENGTH]; //错误:数组必须使用大括号括起来的标识符进行初始化。

EDIT: Thanks!

编辑:谢谢!

3 个解决方案

#1


1  

Something like that

这样的事情

 int** array = new int*[sizeX];
 for(int i = 0; i < sizeX; ++i)
   array[i] = new int[sizeY];

To delete

删除

for(int i = 0; i < sizeX; ++i)
   delete [] array[i];
delete [] array;

#2


0  

If I have understood correctly what you want then the allocation and deallocation will look as

如果我已经正确理解了你想要的东西,那么分配和释放将看起来像

int ** filedata = new int * [LENGTH];

for ( int i = 0; i < LENGTH; i++ ) filedata[i] = new int [LENGTH];

//...

for ( int i = 0; i < LENGTH; i++ ) delete [] filedata[i];
delete [] filedata;

#3


0  

Alternate version to the ones given:

给出的替代版本:

 int** array = new int*[sizeX];
 int *pool = new int[sizeX * sizeY];
 for(int i = 0; i < sizeX; ++i, pool += sizeY)
   array[i] = pool;

To delete:

删除:

 delete [] array[0];
 delete [] array;

The advantage of using this is:

使用它的好处是:

  1. Only two calls to new[] and delete[] are required, regardless of the number of columns. In the previous answer, the number of calls to new and delete depend on the number of columns. This reduces fragmentation, and also will probably give you a speed increase if the number of columns is very large.

    无论列数如何,只需要两次调用new []和delete []。在上一个答案中,对new和delete的调用次数取决于列数。这样可以减少碎片,如果列数非常大,也可能会提高速度。

  2. The data is contiguous. You can access any element in the 2d array from any other element using a simple offset.

    数据是连续的。您可以使用简单的偏移量从任何其他元素访问2d数组中的任何元素。

The disadvantage is that the number of columns for each row needs to be the same, otherwise it becomes very difficult to maintain.

缺点是每行的列数需要相同,否则变得非常难以维护。

#1


1  

Something like that

这样的事情

 int** array = new int*[sizeX];
 for(int i = 0; i < sizeX; ++i)
   array[i] = new int[sizeY];

To delete

删除

for(int i = 0; i < sizeX; ++i)
   delete [] array[i];
delete [] array;

#2


0  

If I have understood correctly what you want then the allocation and deallocation will look as

如果我已经正确理解了你想要的东西,那么分配和释放将看起来像

int ** filedata = new int * [LENGTH];

for ( int i = 0; i < LENGTH; i++ ) filedata[i] = new int [LENGTH];

//...

for ( int i = 0; i < LENGTH; i++ ) delete [] filedata[i];
delete [] filedata;

#3


0  

Alternate version to the ones given:

给出的替代版本:

 int** array = new int*[sizeX];
 int *pool = new int[sizeX * sizeY];
 for(int i = 0; i < sizeX; ++i, pool += sizeY)
   array[i] = pool;

To delete:

删除:

 delete [] array[0];
 delete [] array;

The advantage of using this is:

使用它的好处是:

  1. Only two calls to new[] and delete[] are required, regardless of the number of columns. In the previous answer, the number of calls to new and delete depend on the number of columns. This reduces fragmentation, and also will probably give you a speed increase if the number of columns is very large.

    无论列数如何,只需要两次调用new []和delete []。在上一个答案中,对new和delete的调用次数取决于列数。这样可以减少碎片,如果列数非常大,也可能会提高速度。

  2. The data is contiguous. You can access any element in the 2d array from any other element using a simple offset.

    数据是连续的。您可以使用简单的偏移量从任何其他元素访问2d数组中的任何元素。

The disadvantage is that the number of columns for each row needs to be the same, otherwise it becomes very difficult to maintain.

缺点是每行的列数需要相同,否则变得非常难以维护。