title: Crash when delete 2D pointer matrices[Heap corruption detected]
body: <p>I randomly created two matrices A0 and A1 with a random size. After that, I copy these two matrices to A01 matrix such as</p>
  
  <pre><code>[A0 | 0 ]
  [0  | A1]
  </code></pre>
  
  <p>Finaly, I delete the memory of them. However, I only run about 3-5 times, after that, the program detect the error such as</p>
  
  <p><a href="https://i.stack.imgur.com/AQDFt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AQDFt.png" alt="enter image description here"></a></p>
  
  <p>I am using visual studio 2015. What is happen in my code? Is is wrong to delete the pointer matrices?</p>
  
  <p>This is my full code</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;       /* time */
  #define random(x) (rand()%x)
  typedef unsigned char  U8;
  typedef unsigned int   U32;
  void create_and_delete_matrix()
  {
      U32 M0,M1,M01;
      U32 m_L0,m_L1,m_L01;
      U32 range = 20;
      M0   = random(range) + 1;
      m_L0 = random(range) + 1;
      M1   = random(range) + 1;
      m_L1 = random(range) + 1;
      M01  = M0 + M1;
      m_L01 = m_L0 + m_L1;
  
      U8** A0 = new U8*[M0];
      U8** A1 = new U8*[M1];
      U8** A01 = new U8*[M01];
      //***********For A0***********//
      for (U32 i = 0; i &lt; M0; ++i){
          A0[i] = new U8[m_L0];
          memset(A0[i], 0, m_L0*sizeof(U8));
      }
      //***********For A1***********//
      for (U32 i = 0; i &lt; M1; ++i) {
          A1[i] = new U8[m_L1];
          memset(A1[i], 0, m_L1*sizeof(U8));
      }
      //***********For A01***********//
      for (U32 i = 0; i &lt; M01; ++i) {
          A01[i] = new U8[m_L01];
          memset(A01[i], 0, m_L01*sizeof(U8));
      }
  
      //***********Set random data A0 and A1***********//
      for (U32 i = 0; i &lt; M0; ++i) {
          for (U32 j = 0; j &lt; m_L0; ++j) {
              A0[i][j] = random(2);
              printf("%d ", A0[i][j]);
          }
          printf("\n");
      }
      for (U32 i = 0; i &lt; M1; ++i) {
          for (U32 j = 0; j &lt; m_L1; ++j) {
              A1[i][j] = random(2);
              printf("%d ", A1[i][j]);
          }
          printf("\n");
      }
      //***********Copy A0 and A1 to A01 ***********//
      //***********--------------------- ***********//
      //***********------[A0 | 0 ]-------***********//
      //***********------[0  | A1]-------***********//
      //***********--------------------- ***********//
      for (U32 i = 0; i &lt; M0; ++i){
          //A0 to A01
          memcpy(A01[i] + m_L0, A0[i], m_L0 * sizeof(U8));
      }
      for (U32 i = 0; i &lt; M1; ++i){
          // A1 to A01
          memcpy(A01[i + M0] + m_L0, A1[i], m_L1 * sizeof(U8));
      }
      //**********Print result of A01**********//
      for (U32 i = 0; i &lt; M01; ++i) {
          for (U32 j = 0; j &lt; m_L01; ++j) {
              printf("%d ", A01[i][j]);
          }
          printf("\n");
      }
  
      //free A0, A1, A01 matrix
      for (U32 i = 0; i &lt; M0; ++i){
          if (i &lt; m_L0){
              delete[] A0[i];
              A0[i] = NULL;
          }
      }
      delete[] A0;
      A0 = NULL;
  
      for (U32 i = 0; i &lt; M1; ++i) {
          if (i &lt; m_L1) {
              delete[] A1[i];
              A1[i] = NULL;
          }
      }
      delete[] A1;
      A1 = NULL;
  
      for (U32 i = 0; i &lt; M01; ++i) {
          if (i &lt; m_L01) {
              delete[] A01[i];
              A01[i] = NULL;
          }
      }
      delete[] A01;
      A01 = NULL;
  
  }
  int main(int argc, char **argv) {
      unsigned int time_ui = static_cast&lt;unsigned int&gt;(time(NULL));
      srand(time_ui);
      for (U32 iter = 0; iter &lt; 100; iter++){
          create_and_delete_matrix();
      }
      return 0;
  }
  </code></pre>
  
  <p>This is <a href="http://www.tutorialspoint.com/compile_cpp11_online.php?PID=0Bw_CjBb95KQMSHlkS19JeW4yUWc" rel="nofollow noreferrer">live demo code</a> </p>
  
  <p>I also tested in Ubuntu with g++ and the error is</p>
  
  <pre><code>*** Error in `main': free(): invalid next size (fast): 0x0000000001a58600 ***                                                      
  ======= Backtrace: =========                                                                                                       
  /lib64/libc.so.6(+0x7850e)[0x7ff6e22d750e]                                                                                         
  /lib64/libc.so.6(cfree+0x5b5)[0x7ff6e22e3165]                                                                                      
  main[0x40107b]                                                                                                                     
  main[0x401105]                                                                                                                     
  /lib64/libc.so.6(__libc_start_main+0xf0)[0x7ff6e227efe0]                                                                           
  main[0x400939]                                                                                                                     
  ======= Memory map: ========                                                                                                       
  00400000-00402000 r-xp 00000000 fd:198 59464901                          /home/cg/root/main                                        
  00601000-00602000 r--p 00001000 fd:198 59464901                          /home/cg/root/main                                        
  00602000-00603000 rw-p 00002000 fd:198 59464901                          /home/cg/root/main
  </code></pre>