[英]Dynamic memory allocation - 2 dimensional array
I have some troubles understanding pointers and dynamic memory allocation. 我在理解指针和动态内存分配时遇到了一些麻烦。 I wrote those 2 codes: 我写了这两个代码:
int **array;
array = malloc(nrows * sizeof(int *));
if(array == NULL)
{
fprintf(stderr, "out of memory\n");
return -1;
}
for(i = 0; i < nrows; i++)
{
*(array+i) = malloc(ncolumns * sizeof(int));
if(array[i] == NULL)
{
fprintf(stderr, "out of memory\n");
return -1;
}
}
and: 和:
int **array;
array = malloc(nrows * sizeof(int *));
if(array == NULL)
{
fprintf(stderr, "out of memory\n");
return -1;
}
for(i = 0; i < nrows; i++)
{
array[i] = malloc(ncolumns * sizeof(int));
if(array[i] == NULL)
{
fprintf(stderr, "out of memory\n");
return -1;
}
}
They should allocate space for 2 dimensional array. 他们应该为二维数组分配空间。 Although Im not sure if theyre both correct, I mean: does this line: 尽管我不确定他们是否都正确,但我的意思是:这样做:
array[i] = malloc(ncolumns * sizeof(int));
do the exact same thing line this one: 做与此完全相同的事情:
*(array+i) = malloc(ncolumns * sizeof(int));
? ?
Yes. 是。
array[i]
, *(array+i)
and i[array]
are treated as same things by the compiler. 编译器将array[i]
, *(array+i)
和i[array]
视为相同的事物。
是的,在编译代码时,应该将它们全部视为相同。
Both *(array + i)
and array[i]
evaluate to the same thing (assuming that one is a pointer and the other an integer). *(array + i)
和array[i]
计算结果相同(假设一个是指针,另一个是整数)。 For your sanity and everyone else's, use the second form; 为了您和其他所有人的理智,请使用第二种形式; it'll be easier to read and maintain. 它将更易于阅读和维护。
If you know the size of your array dimensions at compile time, or if you are working with a compiler that supports variable-length arrays , you can simplify this a bit: 如果您知道在编译时数组尺寸的大小,或者正在使用支持可变长度数组的编译器,则可以简化一下:
int (*array)[ncols] = malloc( sizeof *array * nrows );
This will allocate enough memory for an nrows
x ncols
array of int
. 这将为int
的nrows
x ncols
数组分配足够的内存。 If you're working with a compiler that does not support variable-length arrays, then ncols
must be known at compile time (ie, it must be a macro or other compile-time constant). 如果您正在使用不支持可变长度数组的编译器,则必须在编译时知道ncols
(即,它必须是宏或其他编译时常量)。 You would access array elements as you would for any regular 2D array: 您将像访问任何常规2D数组一样访问数组元素:
array[i][j] = x;
One advantage this method has over the two-step method is that all the memory is allocated contiguously ; 与两步方法相比,此方法的一个优点是所有内存都是连续分配的; that is, all the rows will be adjacent in memory. 也就是说,所有行将在内存中相邻。 This can matter if you want to treat the array as a single continuous blob of data (such as if you're sending it over a socket via a write
system call). 如果要将数组视为单个连续的数据块(例如,如果要通过write
系统调用通过套接字将其发送),则这可能很重要。 With the two-step method, it's not guaranteed that all the rows will be adjacent. 使用两步方法,不能保证所有行都相邻。
The other advantage is that deallocation only requires a single call to free
: 另一个优点是释放只需调用一次free
:
free( array );
int **array;
array = malloc(nrows * sizeof(int *));
the variable array is an array of pointer or double pointer variable. 变量数组是指针或双指针变量的数组。 the value of these pointers are done in the next array allocation : 这些指针的值在下一个数组分配中完成:
array[i] = malloc(ncolumns * sizeof(int)); or
*(array+i) = malloc(ncolumns * sizeof(int));
array [i]
==> i is the index of array witch contain the value of pointer allocated by malloc(ncolumns * sizeof(int));
array [i]
==> i是数组的索引,其中包含malloc(ncolumns * sizeof(int));
分配的指针的值malloc(ncolumns * sizeof(int));
*(array + i)
==> is the offset using to parse array and *array + i) is the value of pointer allocated by malloc(ncolumns * sizeof(int));
*(array + i)
==>是用于解析数组的偏移量,而* array + i)是malloc(ncolumns * sizeof(int));
分配的指针的值malloc(ncolumns * sizeof(int));
so 所以
array[i] = malloc(ncolumns * sizeof(int));
and
*(array+i) = malloc(ncolumns * sizeof(int));
are exactly the same 完全一样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.