title: Valgrind Invalid free() / delete / delete[] / realloc() in C
body: <p>Valgrind tells me, that there is a leak in a memory, I've tried to free() it but I suppose that it's not done properly. Any ideas? Thank You.</p>
  
  <blockquote>
    <p>Invalid free() / delete / delete[] / realloc()
    at 0x4C27D4E: free (vg_replace_malloc.c:427)</p>
    
    <p>by 0x400C00: main (main.c:149)</p>
    
    <p>Address 0x51ba138 is 0 bytes after a block of size 8 alloc'd</p>
    
    <p>at 0x4C28BED: malloc (vg_replace_malloc.c:263)
    by 0x400B0E: main (main.c:119)</p>
    
    <p>HEAP SUMMARY:
    in use at exit: 2 bytes in 1 blocks
    total heap usage: 5 allocs, 5 frees, 14 bytes allocated</p>
    
    <p>2 bytes in 1 blocks are definitely lost in loss record 1 of 1</p>
    
    <p>at 0x4C28BED: malloc (vg_replace_malloc.c:263)</p>
    
    <p>by 0x40084F: strdup (main.c:19)</p>
    
    <p>by 0x4009C4: permute (main.c:83)</p>
    
    <p>by 0x400B9C: main (main.c:138)</p>
  </blockquote>
  
  <pre><code>char *strdup (const char *s)
  {
      char *d = malloc (strlen (s) + 1);  // Space for length plus null //line 19
      if (d == NULL) {
          return NULL;            // No memory
      }
      strcpy (d, s);              // Copy the characters
      return d;                   // Return the new string
  }
  
  void permute (char *arrayOfPermutations, int startIndex, int stopIndex,
              char ***permuts)
  {
      int i;
      if (startIndex == stopIndex) {
          **permuts = strdup (arrayOfPermutations);       //save generated string //line 83
          *permuts += 1;          //increment location
      } else {
          for (i = startIndex; i &lt;= stopIndex; i++) {
              swap ((arrayOfPermutations + startIndex),
                  (arrayOfPermutations + i));
              permute (arrayOfPermutations, startIndex + 1, stopIndex, permuts);
              swap ((arrayOfPermutations + startIndex),
                  (arrayOfPermutations + i));
          }
      }
  }
  
  int main (int argc, char *argv[])
  {
      char *stringInput, c = 0;
      unsigned int j = 0, i = 0, stringSize, facto;
      char **permuts, **work;
  
      stringInput = (char *) malloc (sizeof (char));
  
      while (c != '\n') {
          c = getc (stdin);       //read the input from keyboard standard input
          stringInput = (char *) realloc (stringInput, (j + 1) * sizeof (char));  //re-allocate (resize) memory for character read to be stored
          stringInput[j] = c;     //store read character by making pointer point to c
          j++;
      }
  
      stringInput[j - 1] = '\0';  //add null termination
      stringSize = strlen (stringInput);
      facto = factorial (stringSize);
  
      permuts = (char **) malloc (facto * sizeof (char *));       // allocate n! pointers //line 119
      work = permuts;
  
      printf ("String size: %d\n", stringSize);
  
      ...some printfs here...permute (stringInput, 0, stringSize - 1, &amp;work);     //creates permutations of chars //line 138
  
      qsort (permuts, facto, sizeof (char *), compare);   //sorts strings alphabetically
  
      for (i = 0; i &lt;= facto - 1; i++) {
          printf ("\"%s\"\n", permuts[i]);
      }
  
      free (work);                //free the memory //line 149
      free (permuts);             //free the memory
      free (stringInput);         //free the memory
  }
  </code></pre>