title: Deallocating 2D array in C
body: <p>For whatever reason I am getting the following error when trying to free the 2D array I created:</p>
  
  <blockquote>
    <p>Error in `./a.out': free(): invalid next size (fast): 0x0000000001759310 *** Aborted (core dumped)</p>
  </blockquote>
  
  <p>I printed out the contents of the array and they are correct and I am able access all of the elements. However, I am yet to be able to free it. The error occurs when going through the freeing loop, i.e. freeing double *. Would appreciate any help.</p>
  
  <p>Thanks!</p>
  
  <p>Here is the code:</p>
  
  <pre><code>/*allocation*/
  double **block_mat = (double **) malloc (sizeof(double *) * num_blocks);
  int i;
  for (i = 0; i &lt;num_blocks; i++){
      block_mat[i] = (double *) malloc (sizeof(double) * s);
  }
  
  /*freeing*/
  for (i = 0; i &lt; num_blocks; i++){
      free(block_mat[i]);
  }
  </code></pre>
  
  <p>free(block_mat);</p>
  
  <p>EDIT:
  The error was found! I under-allocated memory...So when I printed out my arrays they looked like everything was fine... I allocated arrays of sizes s, but used s^2 instead. Thank you everyone!</p>