title: program causes memory corruption
body: <p>Below is my attempt to write a program which calculates the determinant and the inverse of a nxn matrix. The program returns a valid inverse matrix when 1&lt;=n&lt;=3. However, when n&gt;=4, the program returns `a.out': malloc(): memory corruption (fast) followed by showing the backtrace and the memory map when it trys to calculate the inverse matrix. Could someone tell me how to fix this problem? Thanks in advance.</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct {
    int m, n; double ** entry;
  } MATRIX;
  
  double ** make_2d_array ( int m, int n )
  {
    double ** array = (double**) calloc(m, sizeof(double*));
    int i;
    for (i=0; i&lt;m; ++i)
    { array[i] = (double*) calloc(n, sizeof(double)); }
  
    return array;
  }
  
  MATRIX * make_matrix ( int m, int n )
  {
    MATRIX * matrix = (MATRIX *) calloc(1, sizeof(MATRIX));
    matrix-&gt;m = m; matrix-&gt;n = n;
    matrix-&gt;entry = make_2d_array ( m, n );
    return matrix;
  }
  
  MATRIX * read_matrix ()
  {
    int m, n;
    MATRIX * matrix;
    scanf(&quot;%d %d&quot;, &amp;m, &amp;n );
    matrix = make_matrix ( m, n );
  
    int i, j;
    for (i=0; i&lt;m; i++)
    {
      for (j=0; j&lt;n; j++)
      {
        double x;
        scanf(&quot;%lf&quot;, &amp;x);
        matrix-&gt;entry[i][j] = x;
      }
    }
    return matrix;
  }
  
  MATRIX * del_row_col ( int r, int c, MATRIX * matrix )  //function which removes the rth row and the cth column
  {
    int i,j;
    MATRIX * output = make_matrix ( matrix-&gt;m-1, matrix-&gt;n-1 );
  
    for ( i=r; i&lt;matrix-&gt;m-1; ++i )
    {
      for ( j=c; j&lt;matrix-&gt;n-1; ++j )
      {
        output-&gt;entry[i][j] = matrix-&gt;entry[i+1][j+1]; }
    }
    for ( i=0; i&lt;r; ++i )
    {
      for ( j=0; j&lt;c; ++j )
      {
        output-&gt;entry[i][j] = matrix-&gt;entry[i][j]; }
    }
    for ( i=r; i&lt;matrix-&gt;m-1; ++i )
    {
      for ( j=0; j&lt;c; ++j )
      {
        output-&gt;entry[i][j] = matrix-&gt;entry[i+1][j]; }
    }
    for ( i=0; i&lt;r; ++i )
    {
      for ( j=c; j&lt;matrix-&gt;n; ++j )
      {
        output-&gt;entry[i][j] = matrix-&gt;entry[i][j+1]; }
    }
    return output;
  }
  
  void free_2d_array ( int m, double ** array )
  {
    int i;
    for (i=0; i&lt;m; i++)
    {
      free(array[i]);
    }
    free(array);
  }
  
  void free_matrix ( MATRIX * matrix )
  {
    free_2d_array ( matrix-&gt;m, matrix-&gt;entry );
    free(matrix);
  }
  
  double det( MATRIX * matrix )
  {
    int i;
    if ( matrix-&gt;n == 1 )
    {
      return matrix-&gt;entry[0][0];
    }
    else
    {
      int s=1;
      double d=0;
      for (i=0; i&lt;matrix-&gt;n; i++)
      {
        MATRIX * submatrix = del_row_col ( 0, i, matrix );
        d += s*(matrix-&gt;entry[0][i])*det(submatrix);
        s *= -1;
        free_matrix(submatrix);      //prevents memory leak
      }
      return d;
    }
  }
  
  int power ( int n )  //calculates (-1)^n 
  {
    if ( n%2==0 )
    {
      return 1;
    }
    else if( n%2==1 )
    {
      return -1;
    }
  }
  
  MATRIX * inverse ( MATRIX * matrix )
  {
    MATRIX * inv = make_matrix ( matrix-&gt;m, matrix-&gt;n );
    int i,j;
    if (matrix-&gt;m==1)
    {
      inv-&gt;entry[0][0] = 1/det(matrix);
      return inv;
    }
    else
    {
      for (i=0; i&lt;matrix-&gt;m; ++i)
      {
        for (j=0; j&lt;matrix-&gt;n; ++j)
        {
          double d = 1/det(matrix);
          inv-&gt;entry[i][j] = d*power(i+j)*det( del_row_col ( j, i, matrix ));
        }
      }
      return inv;
    }
  }
  
  void print_matrix ( MATRIX * matrix )
  {
    int i, j;
    printf(&quot;%d %d\n&quot;, matrix-&gt;m, matrix-&gt;n);
    for (i=0; i&lt;matrix-&gt;m; i++)
    {
      for (j=0; j&lt;matrix-&gt;n; j++)
      {
        printf(&quot; %6g&quot;, matrix-&gt;entry[i][j]);
      }
      printf(&quot;\n&quot;);
    }
  }
  
  int main()
  {
    MATRIX * matrix = read_matrix();
    print_matrix(matrix);
    if (matrix-&gt;m != matrix-&gt;n )
    {
      printf(&quot;matrix has no determinant\n&quot;);
    }
    else
    {
      double x = det(matrix);
      printf(&quot;determinant %f\n&quot;, x);
    }
    printf(&quot;inverse \n&quot;);
    if (det(matrix) != 0)
    {
      MATRIX * i = inverse(matrix);
      print_matrix(i);
    }
    else
    {
      printf(&quot;matrix is not invertible\n&quot;);
    }
  }
  </code></pre>
  <p>The program only returns the correct inverse when 1&lt;=n&lt;=3 e.g.</p>
  <pre><code>cat 3x3
  2 0 0 
  0 2 0
  0 0 2
  
  a.out &lt; 3x3
  3 3
        2      0      0
        0      2      0
        0      0      2
  determinant 8.000000
  inverse
  3 3
      0.5     -0      0
       -0    0.5     -0
        0     -0    0.5
  </code></pre>
  <p>when n=4:</p>
  <pre><code>a.out &lt; id
  4 4
        1     -2      2      2
        1     -3      3      4
       -2      5     -4     -6
       -1      3     -6     -2
  determinant -2.000000
  inverse
  *** Error in `a.out': malloc(): memory corruption (fast):...
  </code></pre>