0

I want to allot memory dynamically for a 2D array.

Is there any difference between these two ?

1)

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

2)

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

If yes, what is better to use and why ?

syb0rg
  • 8,057
  • 9
  • 41
  • 81
Vikas Mangal
  • 821
  • 3
  • 10
  • 23

3 Answers3

2

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. 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 **. 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.

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

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[][]?

Carey Gister
  • 141
  • 1
  • 9
0

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 ints. 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().

  • 2
    @ThoAppelsin It definitely is, see [this](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – The Paramagnetic Croissant Jul 27 '14 at 18:33
  • I have no idea who had the liberty to remove my comment like that, I haven't even received any explicit message about it. @TheParamagneticCroiss That link, which I have seen long before already, doesn't support your claim that it is an *error*. It is not a mistake: I think we both agree that his variable `array` is of type `int **`, hence casting the return value of `malloc` to that type is not a mistake. Forgetting to include `stdlib.h` or `memory.h` would be a mistake if he has, casting to a wrong type would be a mistake. Casting `malloc` result to a correct type would never be a mistake. – Utkan Gezer Jul 27 '14 at 18:57