C++ 动态分配 3d 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10238699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:46:38  来源:igfitidea点击:

dynamically allocating 3d array

c++dynamicmultidimensional-array

提问by user974967

I'm a little confused about dynamically allocating a 3d array. Right now, I'm just allocating one big block of memory like so:

我对动态分配 3d 数组有点困惑。现在,我只是像这样分配一大块内存:

int height = 10;
int depth = 20;
int width = 5;

int* arr;
arr = new int[height * width * depth];

Now I'd like to change a value in the 3D array, say:

现在我想更改 3D 数组中的一个值,例如:

//arr[depth][width][height]
arr[6][3][7] = 4;

However, I can't use the above code to change the value. How can I use a single index to access the element at position depth = 6, width = 3, height = 7?

但是,我不能使用上面的代码来更改值。如何使用单个索引访问位置深度 = 6、宽度 = 3、高度 = 7 的元素?

arr[?] = 4;

Is there a better way to dynamically allocate a 3D array?

有没有更好的方法来动态分配 3D 数组?

回答by yaman

C inclined way of doing this is:

C倾向于这样做的方法是:

int ***arr = new int**[X];
for (i = 0; i < z_size; ++i) {
  arr[i] = new int*[Y];
  for (j = 0; j < WIDTH; ++j)
    arr[i][j] = new int[Z];
}

回答by Jesse Good

To index into the flat 3-dimensional array:

要索引到平面 3 维数组:

arr[x + width * (y + depth * z)]

Where x, y and z correspond to the first, second and third dimensions respectively and width and depth are the width and depth of the array.

其中 x、y 和 z 分别对应于第一、第二和第三维,宽度和深度是阵列的宽度和深度。

This is a simplification of x + y * WIDTH + z * WIDTH * DEPTH.

这是 的简化x + y * WIDTH + z * WIDTH * DEPTH

回答by AarCee

To have a simple indexing mechanism like arr[height][width][depth], and to also have default values in the allocated memory to be initialized to 0, please try the following:

要拥有像 arr[height][width][depth] 这样的简单索引机制,并且要在分配的内存中将默认值初始化为 0,请尝试以下操作:

// Dynamically allocate a 3D array
/*  Note the parenthesis at end of new. These cause the allocated memory's
    value to be set to zero a la calloc (value-initialize). */
    arr = new int **[height]();
    for (i = 0; i < height; i++)
    {
        arr[i] = new int *[width]();
        for (j = 0; j < width; j++)
            arr[i][j] = new int [depth]();
    }

And here's the corresponding deallocation:

这是相应的解除分配:

//Dynamically deallocate a 3D array

for (i = 0; i < rows; i++)
{
    for (j = 0; j < columns; j++)
        delete[] arr[i][j];
    delete[] arr[i];
}
delete[] arr;

回答by Sagar Patnaik

Allocation and deallocation for a 3D array (in heap) are exactly the opposite to each other. The key thing to remember, while deallocating the memory properly, is to use the deletekeywords as many times as the newkeyword has been used. Here's my code for initialization and clean up of a 3D array:

3D 数组(在堆中)的分配和释放完全相反。要记住的关键是,在正确释放内存的同时,使用delete关键字的次数与new关键字的使用次数相同。这是我用于初始化和清理 3D 数组的代码:

int ***ptr3D=NULL;
ptr3D=new int**[5];

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

    for(int j=0;j<5;j++)
    {
        ptr3D[i][j]=new int[5]; 

        for(int k=0;k<5;k++)
        {
            ptr3D[i][j][k]=i+j+k; 
        }
    }
}
//Initialization ends here
...
... //Allocation of values

cout << endl <<"Clean up starts here " << endl;

for(int i=0;i<5;i++)
{
    for(int j=0;j<5;j++)
    {
        delete[] ptr3D[i][j];   
    }
    delete[] ptr3D[i];
}
delete ptr3D;

Notice that for 3 newkeywords, 3 corresponding deletekeywords have been used. This should clean up all the memory allocated to the 3D array in heap and Valgrind can be used to verify it in each stage.

请注意,对于 3 个new关键字,使用了 3 个对应的delete关键字。这应该清理分配给堆中 3D 数组的所有内存,并且 Valgrind 可用于在每个阶段对其进行验证。