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 ?

4
  • 1
    Yes, they both are wrong in different ways. And neither one allocates a two-dimensional array. Commented Jul 27, 2014 at 16:55
  • 3
    I would start out by looking at implementing vectors in C. Then go from there. Commented Jul 27, 2014 at 16:57
  • @syb0rg - We are in C land. Not C++ Commented Jul 27, 2014 at 16:58
  • 4
    @EdHeal A lot of C projects end up implementing a vector-like API. Dynamic arrays are such a common need, that it's nice to abstract away the memory management as much as possible with vectors. Commented Jul 27, 2014 at 16:59

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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 Comments

@ThoAppelsin It definitely is, see this.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.