title: Program crashes when trying to free a 2D dynamic allocated array
body: <p>In general it is difficult to understand what and why is happening. 
  In the beginning of the program I declare two 2D dynamic arrays (all the arrays used in the program have the same dimensions):</p>
  
  <pre><code>int **initial_array;
  int **new_array; 
  </code></pre>
  
  <p>Then I allocate the necessary memory:</p>
  
  <pre><code>//dynamic allocate arrays (1st Dimension)
  initial_array = calloc(N,sizeof(int));
  new_array = calloc(N,sizeof(int));
  
  //check if the memory has been allocated correctly
  if (initial_array==NULL || new_array==NULL) 
  {
      printf("Error allocating memory!\n"); //print an error message
      return 1; //return with failure
  }
  
  for (i=0;i&lt;N;i++)
  {
      //dynamic allocate arrays (2nd Dimension)
      initial_array[i] = calloc(P, sizeof(int));
      new_array[i] = calloc(P, sizeof(int));
  }
  </code></pre>
  
  <p>I then call a function that takes the values of the initial array, generates another 2D dynamic array, which I print (inside the function) and free (by calling another function) without a problem, and I store this array to the new_array:</p>
  
  <pre><code>new_array = create_new_array(some_data, initial_array);
  </code></pre>
  
  <p>Then I store the values to the initial_array:</p>
  
  <pre><code>for (i=0;i&lt;N;i++)
  {
      for (p=0;p&lt;P;p++)
      {
          initial_array[i][p] = new_array[i][p];
      }
  }
  
  free_array_in_function(new_array);
  </code></pre>
  
  <p>So far so good. The problem comes when I free the arrays. The initial_array works perfectly:</p>
  
  <pre><code>//free memory
  for (i=0;i&lt;N;i++)
  {
      free(initial_array[i]);
  }
  
  free(initial_array);
  </code></pre>
  
  <p>Then, I try with the new_array but the program crashes:</p>
  
  <pre><code>//free memory
  for (i=0;i&lt;N;i++)
  {
      free(new_array[i]);
  }
  
  free(new_array);
  
  
  ERROR: Unhandled exception at 0x102d12b4 (msvcr90d.dll) in Genetic_v1.exe: 
  0xC0000005: Access violation reading location 0xfeeefee8.
  </code></pre>
  
  <p>Any ideas why is this happening? </p>
  
  <pre><code>//Function
  int **create_new_array(double *some_data, int **individuals_table)
  {
        int **children;
  
       //dynamic allocate array of children (1st Dimension)
       children = calloc(N,sizeof(int));
  
       //check if the memory has been allocated correctly
       if (children==NULL) 
       {
          printf("Error allocating memory!\n"); //print an error message
          return 1; //return with failure
       }
  
       for (cv01=0;cv01&lt;N;cv01++)
       {
          //dynamic allocate array of Individuals (2nd Dimension)
          children[cv01] = calloc(P, sizeof(int));
  
          //check if the memory has been allocated correctly
          if (children[cv01]==NULL) 
          {
             printf("Error allocating memory!\n"); //print an error message
             return 1; //return with failure
          }
      }
  
      //Do some calculations
  
      return children;
  }
  </code></pre>