title: glibc detected, realloc(): invalid old size C
body: <p>Admittedly what I was trying to do was at best educated guess work.</p>
  
  <p>I have an array of strings, and I was trying to account for if someone entered a string that was too large, or the array received too much input (it needs to be dynamic for what I'm trying to do)</p>
  
  <p>here's the section of code that's breaking:</p>
  
  <pre><code>if (strlen(token) &gt; wordlength)
  {
    wordlength *= 2;
    for(int j = 0; j &lt; numwords; j++)
    {
      char* tmpw = realloc(wordarray[j], wordlength);
      assert(tmpw != NULL);
      wordarray[j] = tmpw;
      printf("increased size of words to %zu \n", wordlength);
    }
  
  }
  </code></pre>
  
  <p>Explanations:</p>
  
  <p>Token is the next word being taken in (I'm parsing a string) so I compare it to the current word length, if it's too big, i double word length and try to adjust the array accordingly.</p>
  
  <p>If you need any more information let me know</p>
  
  <p>initialization of wordarray:</p>
  
  <pre><code>wordarray = malloc(numwords);
  for(int i = 0; i &lt; numwords; i++)
    wordarray[i] = malloc(wordlength);
  </code></pre>
  
  <p>Another place where realloc crashes:</p>
  
  <pre><code>if (arraycounter &gt; numwords)
  {
    numwords *= 2;
    char** tmp = realloc(wordarray, numwords);
    assert(tmp != NULL);
    wordarray = tmp;
    for(int h = arraycounter; h &lt; numwords; h++)
      wordarray[h] = malloc(wordlength);
    printf("increased size of wordarray to %zu \n", numwords);
  }
  </code></pre>
  
  <p>In this situation, it would attempt to increase the size of the array if it was about to go over the initial set limit, so would not be affected due to token running out of memory (tested with 20 small words and it crashed on its attempt to resize)</p>