title: How do I free memory in C?
body: <p>I'm writing code which has a lot of 1 &amp; 2 dimensional arrays. I got &quot;error: can't allocate region&quot; and I think its because too much memory is allocated. I use &quot;malloc&quot; and &quot;free&quot; functions, but I'm not sure I'm using them correctly. Maybe you know where I could see good examples on memory management in C?</p>
  <p>so.. I just trying to get one algorithm work and for now this code is just function after function..</p>
  <pre><code>//memory allocation for 1D arrays
  buffer = malloc(num_items*sizeof(double));
  
  //memory allocation for 2D arrays
  double **cross_norm=(double**)malloc(150 * sizeof(double *));
  for(i=0; i&lt;150;i++)
      {
          cross_norm[i]=(double*)malloc(num_items*sizeof(double));
      }
      
      //code
  Window(N, window_buffer);
  STFTforBMP(buffer,N,f, window_buffer);
  getMagnitude(buffer,f, N, magnitude);
  calculateEnergy(flux,magnitude, f);
  calculateExpectedEnergy(expected_flux, candidate_beat_period, downbeat_location, f);
  calculateCrossCorrelation(cross, flux, expected_values, f);
  findLargestCrossCorrelation(&amp;cross_max, cross, f);
  normalizeCrossCorrelation(cross_norm, &amp;cross_max, cross, f);
      ...............
  </code></pre>
  <p>How should I use the <code>free</code> function?</p>