之前我没有真正关注过这个问题,但是我理解为2D数组分配内存最直接的方法是首先创建指针数组然后为每个指针指定一个单独的块,这是非常令人惊讶的。实际数组内容的内存。然后可以在整个存储器中扩展数组,并且访问多行的元素(如A [i] [N-1] - > A [i + 1] [0])可能导致性能损失。
stackoverflow上的某个地方,我找到了一个完美的类C解决方案,在一个malloc调用中分配空间。 但是使用malloc和不安全的指针转换不是一个很好的C ++实践,所以我决定调查我可以google /代码重新创建这个C方法,并构建将存储在单个中的普通2D数组(A [m] [n])记忆块(rowise)。
我想知道这些解决方案是否存在任何问题。我在这里提供了一个在Windows 2015中使用/ Wall在Windows 10下编译的工作代码片段,其中没有关于我的代码的警告。两个函数分配内存,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
答案 0 :(得分:1)
你做了很多无所事事的工作。你开始时声明不正确:
为2D数组分配内存的最直接的方法是[首先]创建指针数组,然后为每个指针分配一个单独的内存块,用于实际的数组内容。
在两种情况下都是:
但是你{{}} {{}}一块记忆,所以上面的1.和2.以上都不适用。
第三个原因是有些人认为你需要这样做:
传递二维数组需要在编译时知道列数:
malloc()
以上:
void Legal(int a[][1000]);
void Illegal(int a[][]);
void PointerVersion(int *a[], int numCols);
有效,因为编译器可以解决如何访问Legal()
; a[1][0]
无法正常工作,因为编译器无法弄清楚如何访问Illegal()
; a[1][0]
很明显 - 在运行时传入列数。您没有收到警告的原因是您在整个地方进行了类型转换 - 特别是PointerVersion()
(不寒而栗!)。肯定有时候需要引用指针指向 - 这不是其中之一!
查看解决方案的另一种方法是:
int **&
以上显示&#34; 2-D阵列&#34;实际上是指向typedef int Array1D[1000]; // 1,000 ints in a 1-D array
typedef Array1D *Array2D[1000]; // 1,000 pointers to 1-D arrays - a 2-D array!(?)
s的1-D数组的一维指针数组。这些需要作为int
(指向指向 - int **
的指针)传递。
唐&#39;吨。只是......不要。如果你知道高度和宽度时需要一个二维整数数组,只需咬住子弹并分配内存块并将其分配给一个int
:
int *
int *array = (int *)malloc(m*n * sizeof(int));
现在是一个指向整数的指针。但是,它也是指向数组开头的指针;或2-D阵列;或三维阵列;或者...这就是为什么上面的#3也是不必要的。将array
传递到任何地方(如有必要,作为int *
),并传递列数。
或者,正如其他人所提到的那样,将所有内容置于int *&
并隐藏所有内容。