title: C99 - 2D dynamic array allocation / reallocation recursively
body: <p>Good morning</p>
  
  <p>before the code, i dont want you guys to give me code for me to use, as i would not learn why it would done like that. I want to learn why this code fails and how to properly use memory allocation so im not to fail again in the future. </p>
  
  <p>The problem i've encountered is related to a function in my code whose objective is, given a 2D dynamic array of chars (Matrix) and another dynamic array of words (List), to find all paths on the Matrix were the words on the list reside, and then store those paths in another dynamic array (Paths). My problem is not on how to find those paths, but on how to store them, as all my attempts on an solution have met with memory reallocation errors.</p>
  
  <p>this is the function in question:</p>
  
  <p>notes:</p>
  
  <p>this code finds a path, stores it into temp and then it SHOULD allocate memory to copy temp to path and then repeat. Without the allocation part of the code, and with a few printfs, the code does show all paths correctly. But showing and storing are 2 different things</p>
  
  <p><code>int ** path  -  the dyn array for the paths</code></p>
  
  <p><code>int ** num_caminhos   -  the number of paths</code></p>
  
  <p><code>char * palavra   -  the word to currently search for</code></p>
  
  <p><code>int ID   -   the ID of the word, for later reference</code></p>
  
  <pre><code>void pesquisa(int ** path, int *num_caminhos, char **matriz, int num_colunas, int num_linhas, char * palavra, int l, int c, int li, int ci, int * temp, int d, int ID){
  if (matriz[l][c]==palavra[d]){
      if ((l-1)&gt;=0 &amp;&amp; palavra[d+1]==matriz[l-1][c]){
          temp[5+d]=1;
          pesquisa(path, num_caminhos, matriz,num_colunas, num_linhas, palavra, l-1, c, li, ci, temp, d+1, ID);}
      if ((l-1)&gt;=0 &amp;&amp; (c+1)&lt;num_colunas &amp;&amp; palavra[d+1]==matriz[l-1][c+1]){
          temp[5+d]=2;
          pesquisa(path, num_caminhos, matriz,num_colunas, num_linhas, palavra, l-1, c+1, li, ci, temp, d+1, ID);}
      if (((c+1)&lt;num_colunas) &amp;&amp; (palavra[d+1]==matriz[l][c+1])){
          temp[5+d]=3;
          pesquisa(path, num_caminhos, matriz,num_colunas, num_linhas, palavra, l, c+1, li, ci, temp, d+1, ID);}
      if ((l+1)&lt;num_linhas &amp;&amp; (c+1)&lt;num_colunas &amp;&amp; palavra[d+1]==matriz[l+1][c+1]){
          temp[5+d]=4;
          pesquisa(path, num_caminhos, matriz,num_colunas, num_linhas, palavra, l+1, c+1, li, ci, temp, d+1, ID);}
      if ((l+1)&lt;num_linhas &amp;&amp; palavra[d+1]==matriz[l+1][c]){
          temp[5+d]=5;
          pesquisa(path, num_caminhos, matriz,num_colunas, num_linhas, palavra, l+1, c, li, ci, temp, d+1, ID);}
      if ((l+1)&lt;num_linhas &amp;&amp; (c-1)&gt;=0 &amp;&amp; palavra[d+1]==matriz[l+1][c-1]){
          temp[5+d]=6;    
          pesquisa(path, num_caminhos, matriz,num_colunas, num_linhas, palavra, l+1, c-1, li, ci, temp, d+1, ID);}
      if ((c-1)&gt;=0  &amp;&amp; palavra[d+1]==matriz[l][c-1]){
          temp[5+d]=7;
          pesquisa(path, num_caminhos, matriz,num_colunas, num_linhas, palavra, l, c-1, li, ci, temp, d+1, ID);}
      if ((l-1)&gt;=0 &amp;&amp; (c-1)&gt;=0 &amp;&amp; palavra[d+1]==matriz[l-1][c-1]){
          temp[5+d]=8;
          pesquisa(path, num_caminhos, matriz,num_colunas, num_linhas, palavra, l-1, c-1, li, ci, temp, d+1, ID);}
  }
  if ((d==strlen(palavra)-1) &amp;&amp; (palavra[0]==matriz[li][ci])){
      path=realloc(path, ((*num_caminhos)+1)*sizeof(int *));
      path[*num_caminhos]=malloc((strlen(palavra)+5)*sizeof(int));
      temp[0]=ID;
      temp[1]=li;
      temp[2]=ci;
      temp[3]=l;
      temp[4]=c;
      for (int i=0; i&lt;5+d; i++){
          path[*num_caminhos][i]=temp[i];
      }
      *num_caminhos=*num_caminhos+1;
      return;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>sorry if the code shows a bit too many "amateur" mistakes :P. I tend to correct them as i learn more. Just a last detail tough, i am sadly forced to use dyn memory allocation as its part of the objective of this code. </p>