简体   繁体   English

2D数组的动态内存分配

[英]Dynamic memory allocation for 2D array

I want to allot memory dynamically for a 2D array. 我想为2D数组动态分配内存。

Is there any difference between these two ? 两者之间有什么区别吗?

1) 1)

array = (int**)malloc(size * sizeof(int*));
for (i = 0; i < size; i++) {
    array[i] = (int *) malloc(size * sizeof(int));
}

2) 2)

array = (int**)malloc(size *size* sizeof(int));

If yes, what is better to use and why ? 如果是,什么更好使用,为什么?

In the first case 在第一种情况下

array = (int**)malloc(size * sizeof(int*));
for (i = 0; i < size; i++) {
    array[i] = (int *) malloc(size * sizeof(int));
}

you are allocating size extents of the size equal to size * sizeof( int ) That is you are allocating size one-dimensional arrays. 您正在分配的size范围等于size * sizeof( int )大小,即您正在分配大小的一维数组。 Accordingly you are allocating size pointers that point to first elements of these one-dimensional arrays. 因此,您正在分配指向这些一维数组的第一个元素的大小指针。

In the second case expression 在第二种情况下

(int**)malloc(size *size* sizeof(int))

means allocation of an extent of size * size of objects of type int and the returned pointer is interpretated as int ** . 表示分配一个size * int类型的对象的大小范围,并将返回的指针解释为int ** So this expression has no sense independing on what is placed in the left side of the assignment. 因此,此表达式不依赖于赋值左侧的内容。 take into account that the size of pointer can be greater than the size of int. 考虑到指针的大小可以大于int的大小。

You could write instead 你可以写

int ( *array )[size] = ( int (*)[size] )malloc(size *size* sizeof(int));

In this case you are indeed allocating a two dimensional array provided that size is a constant expression. 在这种情况下,您确实要分配一个二维数组,前提是size是一个常量表达式。

Those two solutions are very different. 这两种解决方案非常不同。 The first will give you a vector of pointers to vectors. 第一个将为您提供指向向量的指针的向量。 The second will give you a vector of the requested size. 第二个将为您提供所需大小的向量。 It all depends on your use case. 这完全取决于您的用例。 Which do you want? 你要哪个?

When it comes to releasing the memory, the first can only be freed by calling free for each pointer in the vector and then a final free on the vector itself. 释放内存时,只能通过对向量中的每个指针进行释放,然后对向量本身进行最终释放来释放第一个。 The second can be freed with a single call. 第二个可以通过一次调用释放。 Don't have that be your deciding reason to use one or the other. 没有那是您决定使用其中一个的决定性理由。 It all depends on your use case. 这完全取决于您的用例。

What is the type of the object you want to allocate? 您要分配的对象是什么类型? Is it an int **, an int *[] or an int[][]? 是int **,int * []还是int [] []?

I want to allot memory dynamically for a 2 dimensional array. 我想为二维数组动态分配内存。

Then just do 然后就做

int (*arr)[size] = malloc(size * sizeof *arr);

Is there any difference between these two ? 两者之间有什么区别吗?

Yes, they are wrong because of different errors. 是的,由于错误不同,它们是错误的。 The first attempt does not allocate a 2D array, it allocates an array of pointers and then a bunch of arrays of int s. 第一次尝试不分配2D数组,而是分配一个指针数组,然后分配一堆int数组。 Hence the result will not necessarily be contiguous in memory (and anyway, a pointer-to-pointer is not the same thing as a two-dimensional array.) 因此结果不一定在内存中是连续的(无论如何,指针到指针与二维数组不是同一回事。)

The second piece of code does allocate a contiguous block of memory, but then you are treating it as if it was a pointer-to-pointer, which is still not the same thing. 第二段代码确实分配了一个连续的内存块,但是随后您将其视为指针对指针,这仍然是不一样的。

Oh, and actually, both snippets have a common error: the act of casting the return value of malloc() . 哦,实际上,两个片段都有一个共同的错误:强制转换malloc()返回值的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM