title: Allocate and free 2D array in C using void
body: <p>The idea is to use only two functions that can both allocate and free a 2D array of a given data type that is not known at compile-time. The code I have written crashes in the freeing function, but I cannot see why. Here is the code:</p>
  
  <pre><code>void** new2DArray(int row, int col, int typeSize){
      void** temp = malloc(sizeof(void*)*row); //EDIT                     
      for (row-=1; row &gt;= 0; row--)                               
          temp[row] = malloc(typeSize*col);
      return temp; 
  }
  
  
  void free2DArray(void** array, int row, int typeSize){
      for (row -= 1; row &gt;= 0; row--)
          free( *(array + row*typeSize) );
       free(array); 
  }
  </code></pre>
  
  <p>Where, for a 3x3 array, is would be called like:</p>
  
  <pre><code>double** test = (double**) new2DArray(3, 3, sizeof(double));
  free2DArray(test, 3, sizeof(double));
  </code></pre>
  
  <p>Is there anything immediately wrong with the freeing function? </p>