动态分配/释放2D& 3D阵列 [英] dynamic allocation/deallocation of 2D & 3D arrays

查看:183
本文介绍了动态分配/释放2D& 3D阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有关动态分配/取消分配2D数组的算法,但是对于3D阵列我并不太了解。

使用这种知识和一些对称性,我想出了以下代码。

(在编码时我很难在3D中进行可视化)。



请评论正确性,并建议任何更好的选择(效率或直觉),如果有的话。

另外,我认为这两个2D像数组arr2D [2] [3]和

arr3D [2] [3] [2]这样的静态数组就可以正常访问3D数组。对?



2D代码

  //分配2D数组
int ** allocate2D(int rows,int cols)
{
int ** arr2D;
int i;

arr2D =(int **)malloc(rows * sizeof(int *)); (i = 0; i< rows; i ++)
{
arr2D [i] =(int *)malloc(cols * sizeof(int));
}
}

//取消分配2D数组
void deallocate2D(int ** arr2D,int rows)
{
int i ; (i = 0; i< rows; i ++)
{
free(arr2D [i]);


}

免费(arr2D);
}

3D代码

  //分配一个3D数组
int *** allocate3D(int l,int m,int n)
{
int *** arr3D ;
int i,j,k;

arr3D =(int ***)malloc(l * sizeof(int **));

for(i = 0; i {
arr3D [i] =(int **)malloc(m * sizeof(int *));
for(j = 0; j< m; j ++)
{
arr3D [i] [j] =(int *)malloc(n * sizeof(int));
}
}

return arr3D;
}

//取消分配3D数组
void deallocate3D(int arr3D,int l,int m)
{
int i,j; (i = 0; i

{
for(int j = 0; j< m; j ++)
{
自由(arr3D [i] [j]);
}
free(arr3D [i]);
}
free(arr3D);
}


解决方案

您还可以分配一个数组并计算个体指数。这需要更少的分配器调用,并且导致更少的碎片和更好的缓存使用。

  typedef struct {
int a;
int b;
int * data;
} Int2d;

Int2d arr2d = {2,3};
arr2d.data = malloc(arr2d.a * arr2d.b * sizeof * arr2d.data);

现在 arr2d [r] [c] 成为 arr2d.data [r * arr2d.b + c] 。交易是一个单独的()离开。作为一个奖励,你一定会永远保持你的动态数组大小。



Extrapolating to 3d:

  typedef struct {
int a;
int b;
int c;
int * data;
} Int3d;

Int3d arr3d = {2,3,4};
arr3d.data = malloc(arr3d.a * arr3d.b * arr3d.c * sizeof * arr3d.data);

// arr3d [r] [c] [d]
//变为:
arr3d.data [r *(arr3d.b * arr3d.c)+ c * arr3d.c + d];

您应该将这些索引操作(以及该事项的(除)分配)封装在一个单独的函数或宏。



(r,c和d的名称可能更好,我正在进行行,列和深度,而a,b和c是它们相应尺寸的极限,你可能喜欢这样的n1,n2,n3,甚至使用一个数组。)


I know about algorithms to allocate/deallocate a 2D array dynamically, however I'm not too sure about the same for 3D arrays.
Using this knowledge and a bit of symmetry, I came up with the following code.
(I had a hard time visualizing in 3D during coding).

Please comment on the correctness and suggest any better alternative (efficiency-wise or intuitively), if any.
Also, I think both these 2D and 3D arrays can be accessed normally like static arrays like arr2D[2][3] and
arr3D[2][3][2]. Right?

Code for 2D

//allocate a 2D array
int** allocate2D(int rows,int cols)
{
    int **arr2D;
    int i;

    arr2D = (int**)malloc(rows*sizeof(int*));
    for(i=0;i<rows;i++)
    {
        arr2D[i] = (int*)malloc(cols*sizeof(int));
    }
}

//deallocate a 2D array
void deallocate2D(int** arr2D,int rows)
{
    int i;

    for(i=0;i<rows;i++)
    {
        free(arr2D[i]);
    }

    free(arr2D);
}  

Code for 3D

//allocate a 3D array
int*** allocate3D(int l,int m,int n)
{
int ***arr3D;
int i,j,k;

arr3D = (int***)malloc(l * sizeof(int **));

for(i=0;i<l;i++)
{
    arr3D[i] = (int**)malloc(m * sizeof(int*));
    for(j=0;j<m;j++)
    {
        arr3D[i][j] = (int*)malloc(n*sizeof(int));
    }
}

return arr3D;
}

//deallocate a 3D array
void deallocate3D(int arr3D,int l,int m)
{
    int i,j;

    for(i=0;i<l;i++)
    {
        for(int j=0;j<m;j++)
        {
            free(arr3D[i][j]);
        }
        free(arr3D[i]);
    }
    free(arr3D);
}

解决方案

You can also allocate one array and compute individual indices. This requires fewer allocator calls and results in both less fragmentation and better cache use.

typedef struct {
  int a;
  int b;
  int* data;
} Int2d;

Int2d arr2d = { 2, 3 };
arr2d.data = malloc(arr2d.a * arr2d.b * sizeof *arr2d.data);

Now arr2d[r][c] becomes arr2d.data[r * arr2d.b + c]. Deallocation is a single free() away. As a bonus you're sure to always keep your dynamic array sizes with you.

Extrapolating to 3d:

typedef struct {
  int a;
  int b;
  int c;
  int* data;
} Int3d;

Int3d arr3d = { 2, 3, 4 };
arr3d.data = malloc(arr3d.a * arr3d.b * arr3d.c * sizeof *arr3d.data);

//arr3d[r][c][d]
// becomes:
arr3d.data[r * (arr3d.b * arr3d.c) + c * arr3d.c + d];

You should encapsulate these index operations (and the (de-)allocations for that matter) in a separate function or macro.

(The names for r, c, and d could be better—I was going for row, column, and depth. While a, b, and c are the limits of their corresponding dimensions, you might prefer something like n1, n2, n3 there, or even use an array for them.)

这篇关于动态分配/释放2D&amp; 3D阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆