Valgrind Invalid free() / delete / delete[] / realloc() in C

Before I read your code. Valgrind reporting invalid free error is not because of leaked memory, but you try to free invalid memory (or free same memory more than once). Compiling your code with the compilter flag -g (gdb debug), will enable more debug information, making the backtraces from valgrind much better.

A quick guess:

  for(i = 0; i <= facto-1; i++)
    printf("\"%s\"\n", permuts[i]);
    free (permuts[i]); /* free the text, allocated with strdup(); */

  free(permuts);//free the memory
  free(stringInput);//free the memory

Leave a Comment