问题描述
我了解的算法来分配/释放动态二维数组,但我不是太肯定同为3D阵列。结果
利用这些知识,有点对称性,我想出了以下code。结果
(我有一个很难的3D可视化编码过程中)。
请上的正确性,如果任何意见和建议任何更好的选择(效率明智或直觉)。结果
另外,我觉得这两个二维和三维数组可以像正常一样arr2D [2] [3]和结果静态数组访问
arr3D [2] [3] [2]。对?
code为2D
//分配一个二维数组
INT ** allocate2D(INT行,诠释COLS)
{
INT ** arr2D;
INT I; arr2D =(INT **)的malloc(行*的sizeof(INT *));
对于(i = 0; I<行;我++)
{
arr2D [I] =(INT *)malloc的(COLS *的sizeof(INT));
}
}//释放一个二维数组
无效deallocate2D(INT ** arr2D,诠释行)
{
INT I; 对于(i = 0; I<行;我++)
{
免费(arr2D [I]);
} 免费(arr2D);
}
code为3D
//分配一个三维阵列
INT *** allocate3D(int类型l,诠释男,INT N)
{
INT *** arr3D;
INT I,J,K;arr3D =(INT ***)的malloc(L * sizeof的(INT **));对于(i = 0; I<升;我++)
{
arr3D [I] =(INT **)的malloc(M *的sizeof(INT *));
为(J = 0; J<米; J ++)
{
arr3D [I] [J] =(INT *)malloc的(N * sizeof的(INT));
}
}返回arr3D;
}//释放一个三维阵列
无效deallocate3D(INT arr3D,诠释L,INT M)
{
INT I,J; 对于(i = 0; I<升;我++)
{
对于(INT J = 0; J<米; J ++)
{
免费(arr3D [I] [J]);
}
免费(arr3D [I]);
}
免费(arr3D);
}
您还可以分配一个数组,并计算出各个指标。这需要更少的分配呼叫和两个碎片更少结果和更好的缓存使用。
typedef结构{
int类型的;
INT B:
为int *的数据;
} Int2d;Int2d arr2d = {2,3};
arr2d.data =的malloc(arr2d.a * arr2d.b * sizeof的* arr2d.data);
现在 arr2d [R] [C]
变成 arr2d.data [R * arr2d.b + C]
。释放是一个免费的()的距离。作为奖励,你一定要始终保持你的动态数组的大小和你在一起。
外推到3D:
typedef结构{
int类型的;
INT B:
INT℃;
为int *的数据;
} 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的名称可以更好—我要去为行,列,和深度虽然,b和c是其相应的尺寸的限制,则可能preFER东西。像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&放释放;三维阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!