I'm not sure this is the issue, but your freeRecursively
should definitely not ptr = root;
(causes some "use after free").
BTW, if (ptr->children[i] == NULL)
is pretty useless, as then free(ptr->children[i]);
is same as free(NULL);
, does nothing.
Another problem I see (probably "double free") is the free
s in unload
. They should not be there, freeRecursively
should handle all the nodes within the trie.
Also, both in load
and check
, for the null terminator you create another node, but you should only mark the last one (it works partly because you do the same mistake in both places, but due to i
being declared inside the loop, causing a "use of uninitialised variable", it's also a bit of luck, and it might be connected to your segfault). One way to fix this would be to use for (int x = 0; x < a; x++)
without the + 1
in both functions, and in load
have the path->is_word = true; wordCOUNT++;
after that inner loop, like you test path->is_word
after the loop in check
.
I would definitely recommend checking with valgrind
as mentioned in the hints. It is a bit hard to read at first, but sometimes points to issues before they crash the programme.