title: Dynamic Allocation For A 2D Array Results in Memory Leak
body: <p>I am implementing a 2D Matrix with 2 columns for each row, for storing data came from a text file, which I don't know the number of lines. Because I dont know the size, I am trying to reallocate whenever it's needed by the following implementation.</p>
  <pre><code>typedef struct {
      int **array;
      size_t used;
      size_t size;
  } Array;
  
  void initArray(Array *a, size_t initialSize) {
      a-&gt; array = (int **)malloc(initialSize * sizeof(int *));
      for (int i = 0; i &lt; initialSize; i++)
      {
          a-&gt;array[i] = (int *) malloc(2 * sizeof(int));
      }
      a-&gt;used = 0;
      a-&gt;size = initialSize;
  }
  void insertArray(Array *a, int firstValue, int secondValue) {
  
      if (a-&gt;used == a-&gt;size) {
          a-&gt;size *= 2;
          *a-&gt;array = (int *) realloc(a-&gt;array, a-&gt;size * sizeof(int));
          for (int i = a-&gt;used; i &lt; a-&gt;size; i++)
          {
              a-&gt;array[i] = (int *) malloc(2 * sizeof(int *));
          }
      }
      a-&gt;array[a-&gt;used][0] = firstValue;
      a-&gt;array[a-&gt;used][1] = secondValue;
      a-&gt;used++;
  }
  void freeArray(Array *a)
  {
      for(int i=0; i &lt; a-&gt;size; i++)
      {
          free(a-&gt;array[i]);
      }
      free(a-&gt;array);
      *a-&gt;array = NULL;
      a-&gt;used = 0;
      a-&gt;size = 0;
  }
  //For easy debugging I added a dummy main
  int main(int argc, char *argv[]) {
      Array array;
      initArray(&amp;array, 5);
      for(int i=0; i&lt;123; i++)
      {
          insertArray(&amp;array, i, i+1);
      }
      freeArray(&amp;array);
  }
  </code></pre>
  <p>So my aim is clearly to expand the array by allocating 1x more memory whenever its needed. But there are errors in my implementations that I can't find.</p>
  <p>1-) I create an array, and fill it with less than <strong>initialSIze</strong> , and free it using the function. It looks fine, but if i check with valgrind, I get the following issue, but it says no memory leak:</p>
  <pre><code>==11887== Invalid write of size 8
  ==11887==    at 0x109449: freeArray (in /home/qweqwe/CLionProjects/untitled/main)
  ==11887==    by 0x1095DE: main (in /home/qweqwe/CLionProjects/untitled/main)
  ==11887==  Address 0x4a4e260 is 0 bytes inside a block of size 40 free'd
  ==11887==    at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==11887==    by 0x109441: freeArray (in /home/qweqwe/CLionProjects/untitled/main)
  ==11887==    by 0x1095DE: main (in /home/qweqwe/CLionProjects/untitled/main)
  ==11887==  Block was alloc'd at
  ==11887==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==11887==    by 0x10926D: initArray (in /home/qweqwe/CLionProjects/untitled/main)
  ==11887==    by 0x109505: main (in /home/qweqwe/CLionProjects/untitled/main)
  </code></pre>
  <p>2-) When I try to add one more than <strong>initialSize</strong>, the first pair of elements (array-&gt;array[0][0], array-&gt;array[0][1]) are replaced with some random numbers, (I am freeing them by mistake?). And when I do a valgrind check, returns a ton of error messages including a memory leak. If I add even more, I got this error:</p>
  <pre><code>free(): double free detected in tcache 2
  Aborted (core dumped)
  </code></pre>
  <p>So whats the issue there? I am allocating memory wrong somehow? Then how should i do it?</p>
  <p>P.S: My first question is solved with removing the * from a-&gt;array = NULL; at freeArray function. Please help me with second issue...</p>