title: Deleting 2d array in C++
body: <p>Can some one tell me what is wrong in the for loop? When I run it, it interrupts. I tried to debug to see what is wrong, I noticed that in the for loop it just stops:</p>
  
  <pre><code>#define MAX_POPULATION 64
  float **tr_pop;//Tournament candidates
  float **matingPool;//Mating pool
  tr_pop=new float *[m];
  matingPool=new float *[m];
  for(l=0;l&lt;m+1;l++)//allocating
  {
      tr_pop[l]=new float[MAX_POPULATION];
      matingPool[l]=new float[MAX_POPULATION];
  }
  for ( int r = 0; r &lt; row; ++r )//deleting
  {
   delete [] matingPool[r];//Stops here (not ending program just frozen)
   delete [] tr_pop[r];
  }
  delete [] tr_pop;
  delete [] matingPool;
  </code></pre>
  
  <p>=======OK. PROBLEM SOLVED=======</p>
  
  <p>Here is the reason:</p>
  
  <p>I just changed the <code>MAX_POPULATION</code> into the <code>MAX_POPULATION+1</code> and it worked.</p>
  
  <pre><code>for(l=0;l&lt;m+1;l++)
      {
      tr_pop[l]=new float[MAX_POPULATION+1];
      matingPool[l]=new float[MAX_POPULATION+1];
      }
  </code></pre>
  
  <p>Because in another function I think I was doing violation:</p>
  
  <pre><code>void crossover()
  {
  int p1,p2,i,j;float tempBit;
  p1=m/3;
  p2=(2*m)/3;
  for(j=0;j&lt;MAX_POPULATION;j++)
  {
      for(i=p1;i&lt;p2;i++)
      {
      tempBit=matingPool[i][j];
      matingPool[i][j]=matingPool[i][j+1];//THE VIOLATION POINT (I THINK)
      matingPool[i][j+1]=tempBit;
      }
      j++;
  }
  </code></pre>
  
  <p>As you can see, when <code>j = MAX_POPULATION</code> at the end of the loop, i was trying to reach <code>MAX_POPULATION + 1</code>. So I changed the allocations for columns, and the problem solved :)</p>