0

I've come pretty close to finishing speller (I think)... my code seems to be working and can find all the misspelled words, but then I also get a "Segmentation fault" underneath. I've tried using help50 valgrind, but only got this back "Sorry, help50 does not yet know how to help with this!"

If it's not too much of a hassle, could you guys look into my code to help me understand what's wrong with it?

Thanks a lot in advance!

Edit: problem solved

1 Answer 1

1

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 frees 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.

2
  • Thanks a ton Blauelf!!! I was using strlen() + 1 because I wanted to count the number of '\0's for words. But I'll try to look into the things you've pointed out. Thanks again!
    – chi.bui191
    Commented Nov 4, 2019 at 15:47
  • Thanks again Blauelf. I've adjusted the code as per your suggestions and it IS working!!!
    – chi.bui191
    Commented Nov 5, 2019 at 5:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .