【发布时间】:2016-07-01 10:39:32
【问题描述】:
我之前并没有真正关注过这个问题,但让我感到非常惊讶的是,为 2D 数组分配内存的最直接方法是首先创建指针数组,然后为每个指针分配一个单独的块实际数组内容的内存。然后可以将数组分布在整个内存中,访问多行的元素(如 A[i][N-1] -> A[i+1][0])可能会导致性能损失。
在stackoverflow 的某个地方,我找到了一个完美的类 C 解决方案,可以在一次 malloc 调用中分配空间。 但是使用 malloc 和不安全的指针转换不是一个好的 C++ 实践,所以我决定研究我可以用谷歌/代码重新创建这个 C 方法并构造普通的 2D 数组 (A[m][n]),这些数组将存储在单个内存块(逐行)。
我想知道此类解决方案是否存在任何问题。我在这里展示了一个工作代码 sn-p,它是在 VS 2015 中的 Windows 10 下编译的,其中 /Wall 没有给出关于我的代码的警告。两个函数分配内存,类 C 和类 C++。我还为 x86 和 x64 编译了它,示例日志可以在代码部分之后找到。
来源:
#include<stdio.h>
#include<stdlib.h>
void allocate(int m, int n, int** & arr)
{
// Allocates memory that is sufficient for storing m pointers to rows plus m*n continuos array
arr = (int**)malloc(m * sizeof(int*) + m*n * sizeof(int));
// First block of memory (m*sizeof(int*)) should be assigned with pointers to rows from second block of memory
for (int i = 0; i < m; i++)
{
// arr is int**, arr + 1 shift pointer for sizeof(int*) bytes
// arr + m points to the beginning of second (array) memory block, but it is of int** type;
// consider it as a pointer to a single block of (int*), thus cast
*(arr + i) = (int*)(arr + m) + n*i;
//(int*)(arr + m) is the beginning of array, + n*i shifts to i-th row, j - iterates along that row
// finally, * allows to zero memory at that address
for (int j = 0; j < n; j++)
*((int*)(arr + m) + n*i + j) = 0;
}
}
void allocatecpp(int m, int n, int** & p)
{
// Uses reinterpret_cast<T> as it performs casts from int** to int*, which are not allowed with normal casts
// Allocates memory that is sufficient for storing m pointers to rows plus m*n continuos array; function ::operator new(size)
p = reinterpret_cast<int**> (::operator new(m*n * sizeof(int) + m*sizeof(int*)));
// Iterates through first block of memory, assigning addresses of respective rows
for (int i = 0; i < m; i++)
{
// Logic is the same as in C-style, but with some C++ stuff
*(p + i) = reinterpret_cast<int*>(p + m) + n*i;
for (int j = 0; j < n; j++)
// Zeros memory
*(reinterpret_cast<int*>(p + m) + n*i + j) = 0;
}
}
int main()
{
// Define size of an array; array of m rows x n columns
int m = 5, n = 4;
// array pointer, not sure how to use smart pointer here
int** arr;
// Allocates memory
allocatecpp(m, n, arr);
// Normally iterates through a 2D array, assigning values that correspond to the element position; e.g. element [3][2] gets value 43
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
arr[i][j] = 10 * (i+1) + (j+1);
// Address of the beginning; value of arr treated as a pointer
printf("Address of the beginning: 0x%p\r\n", arr);
// Moves along row address space and asks for addresses of a specific row
for (int i = 0; i < m; i++)
printf("Row %i at address 0x%p pointing to 0x%p\r\n", i, arr+i, *(arr + i));
printf("\r\n");
// A pointer to an actual block of memory that represents array as continuous memory piece; casted to int*
int* p = (int*)(arr + m);
// Iterates through the whole array and prints addresses of elements and values (which correspond to position in matrix)
for (int i = 0; i < m * n; i++)
printf("0x%p\t%02i\r\n", p + i, *(p + i));
delete (arr);
return 0;
}
x86 输出:
Address of the beginning: 0x00DADD18
Row 0 at address 0x00DADD18 pointing to 0x00DADD2C
Row 1 at address 0x00DADD1C pointing to 0x00DADD3C
Row 2 at address 0x00DADD20 pointing to 0x00DADD4C
Row 3 at address 0x00DADD24 pointing to 0x00DADD5C
Row 4 at address 0x00DADD28 pointing to 0x00DADD6C
0x00DADD2C 11
0x00DADD30 12
0x00DADD34 13
0x00DADD38 14
0x00DADD3C 21
0x00DADD40 22
0x00DADD44 23
0x00DADD48 24
0x00DADD4C 31
0x00DADD50 32
0x00DADD54 33
0x00DADD58 34
0x00DADD5C 41
0x00DADD60 42
0x00DADD64 43
0x00DADD68 44
0x00DADD6C 51
0x00DADD70 52
0x00DADD74 53
0x00DADD78 54
x64 输出
Address of the beginning: 0x000001D9E0FB4DE0
Row 0 at address 0x000001D9E0FB4DE0 pointing to 0x000001D9E0FB4E08
Row 1 at address 0x000001D9E0FB4DE8 pointing to 0x000001D9E0FB4E18
Row 2 at address 0x000001D9E0FB4DF0 pointing to 0x000001D9E0FB4E28
Row 3 at address 0x000001D9E0FB4DF8 pointing to 0x000001D9E0FB4E38
Row 4 at address 0x000001D9E0FB4E00 pointing to 0x000001D9E0FB4E48
0x000001D9E0FB4E08 11
0x000001D9E0FB4E0C 12
0x000001D9E0FB4E10 13
0x000001D9E0FB4E14 14
0x000001D9E0FB4E18 21
0x000001D9E0FB4E1C 22
0x000001D9E0FB4E20 23
0x000001D9E0FB4E24 24
0x000001D9E0FB4E28 31
0x000001D9E0FB4E2C 32
0x000001D9E0FB4E30 33
0x000001D9E0FB4E34 34
0x000001D9E0FB4E38 41
0x000001D9E0FB4E3C 42
0x000001D9E0FB4E40 43
0x000001D9E0FB4E44 44
0x000001D9E0FB4E48 51
0x000001D9E0FB4E4C 52
0x000001D9E0FB4E50 53
0x000001D9E0FB4E54 54
【问题讨论】:
-
当您可以实现一个表示二维数组的类时,是否有充分的理由去做所有这些事情?
-
另外,如果你想让你的代码被审查,那么最好的地方是:codereview.stackexchange.com
-
@juanchopanza,是的,你可以实现一个类,它将数组存储在连续的内存块中,并有一些运算符重载来访问元素,但在这里我尝试明智地创建最简单的数组。它不一定是一个适用的解决方案,更像是一个理论上的解决方案——如果他愿意,应该由谁去做。
-
“为 2D 数组分配内存的最直接方法是首先创建指针数组” 不,这是广泛使用但不正确和不好的做法。因此,整个程序没有多大意义,您正在创建复杂性增加的慢代码,却一无所获。就 C 而言,您的问题与How do I correctly set up, access, and free a multidimensional array in C? 重复。在 C++ 中类似:
new type[x][y]. -
@Ilia 但是使用 malloc 和不安全的指针转换不是一个好的 C++ 实践 -- 这就是为什么使用templates。
标签: c++ c arrays memory-management