# Thread: This code results in a memory leak, right?

1. ## This code results in a memory leak, right?

Code:
```void free_2D(double **array)
{
free(array[0]);
free(array);
}```
Assuming that we have already allocated a 2D array of doubles with both dimensions > 1, using this to free it will result in a memory leak, yes?

Because I was sure that you needed to loop over the second dimensions and free each array[i] separately before freeing array, but this is part of a code that seems to work. So am i wrong about how things are freed, or is it working only because it coincidentally doesnt run out of memory?

it should be (I think?)

Code:
```void free_2D(double **array, int xdim)
{
int i = 0;
for (i = 0; i < xdim; i++)
{
free(array[i]);
}
free(array)
}```

2. It looks like a possible memory leak, but what is the code that allocates the memory? You need that to be certain.

3. Code:
```void* calloc_2D(int N0, int N1)
{
int i;
double **array;

if( ( array = (double **) calloc( N0, sizeof(double *)) ) == NULL ){
fprintf(stderr,"error in calloc_2D() \n");
return NULL;
}

if( ( array[0] = (double *) calloc( N1 * N0, sizeof(double))) == NULL ){
fprintf(stderr,"error in calloc_2D() \n");
return NULL;
}

for( i=0; i<N0; ++i){

array[i] = array[0] + i*N1; // pointer arithmetic
}

return array;

}```

4. Observe that there are only two calls to calloc(). Therefore, the two calls to free() make sense, and in fact are correct.

You are probably thinking of a case where for each pointer to double, a calloc() is used, but here the idea is to use calloc() once to allocate enough memory for the entire 2D array, and then use a loop to get the pointers to double to point to the correct double in this 1D array, and thereby allow one to access the 1D array as a 2D array.

5. Huh, never seen it done that way before. Are there advantages to using a 1D array as opposed to a 2D array?

6. Originally Posted by KBriggs
Are there advantages to using a 1D array as opposed to a 2D array?
In a way, you are using a 2D array, and in a way, you aren't. The same goes for a solution that uses malloc()/realloc()/calloc() for each pointer to double.

As for the advantage: obviously, you have fewer calls to functions that allocate memory.

7. Cool, I wouldn't have thought you could access array elements with the notation array[i][j] when it was alloced this way, but apparently you can do that too. Seems this is quite a bit more efficient than what I have been doing up to now.

You learn something new every day

8. One could even get it down to one malloc if you allocated a chunk big enough for the array of pointers plus the array of items, and then set those pointers in the array to point to further along in the block where each row started.

However, if one cares that much about memory locality and compactness, it's probably better to simply simulate a 2D array bitmap-style, where there's no need to allocate pointers at all.

9. I was actually thinking of previous programs I have written where I need to realloc large arrays many times within a loop. It might save a bit of time for larger data sets. Then again, I have no idea how much computation time malloc and related functions actually take, so it might be a useless optimization. I'll have to try and see.