Question: I am somewhat new to C programming. I have a doubt regarding dynamic memory allocation. The following is a code in the main program for memory allocation.

```
double **mat=(double**)malloc(sizeof(double*)*n);
mat[0]=(double*)calloc(sizeof(double),n*n);
for(i=1; i<n; i++)
mat[i] = mat[i-1] + n;
mat = create_square_matrix(n);

```

I want to call the function and create elements in the matrix inside the function. Do I have once again allocation memory inside the function like below or Is there any other method to avoid this tedious memory allocation repetition. Following is the function.

```
`double** create_square_matrix(int n)
{
int i,j,sum=0;
double **array2=(double**)malloc(sizeof(double*)*n);
array2[0]=(double*)calloc(sizeof(double),n*n);
for(i=1; i<n; i++)
array2[i] = array2[i-1] + n;
for (i = 0; i < n; ++i)
  {
      for (j = 0; j < n; ++j)
      {
        sum=sum+j;
        array2[i][j]=sum;
      }
  }
return array2;
}

```

`
The above function returns the array which is stored in the 'mat' variable. And another question is how do I free the memory of variable 'array2' inside the function after using the return method. I can't free the memory possibly before returning the array. Is there a method to free the memory in the above function.

Answer: 
Your function create\_square\_matrix allocates memory and then fills it by some values.

Your top piece of code allocates memory, and then calls create\_square\_matrix which again allocates memory. It is like to mop floors before calling the janitor who also mops floors. You don't need to allocate memory twice. Not only it is unneccessary, in fact it is bad. Since you perform two allocations, the memory from the first one is lost, and there is no way to free it. This is called memory leak. Instead of

```
double **mat=(double**)malloc(sizeof(double*)*n);
mat[0]=(double*)calloc(sizeof(double),n*n);
for(i=1; i<n; i++)
mat[i] = mat[i-1] + n;
mat = create_square_matrix(n);

```

you should write

```
double **mat = create_square_matrix(n);

```
Using a nice function macro for memory allocation is always a good idea. Unless you have to free memory manually I would leave it to a garbage collector such as libgc. Below is an example. If you don't want to use a garbage collector you can just replace *GC\_MALLOC* with *malloc*. When you free the array (manually) you must first free the individual rows.

```
#include <gc/gc.h>
#include <stdio.h>
#include <stdlib.h>

#define NEW_ARRAY(ptr, n) (ptr) = GC_MALLOC((n) * sizeof (ptr)[0])

double **SquareMatrix(int n)
{
    double **A;
    int i, j;

    NEW_ARRAY(A, n);
    for (i = 0; i < n; i++) {
        NEW_ARRAY(A[i], n);
        for (j = 0; j < n; j++) {
            A[i][j] = 0.0;
        }
    }
    return A;
}

int main(void)
{
    const int n = 5;
    double **A;
    int i, j;

    A = SquareMatrix(n);    
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            printf("%5.2f ", A[i][j]);
        }
        putchar('\n');
    }

    return 0;
}

```