following problem:

Say I have a dictionary (called mysmall.txt) with only those words in it:


And I have a text file (mytext.txt) with the exact same content as in mysmall.txt

Now, when executing speller.c, thus checking "misspelled words" in mytext.txt, only foobar is correct. Like, as the other words are not stored anymore in the trie.

Using GDB I see that current_node->is_word = true; is executed properly for all words. So, somehow the last word ovewrites the other trie nodes or so?!

Here is the relevant code, which should contain this mean bug:

[mod: code hidden behind edit flag to avoid spoilers]

1 Answer 1


It's possible that I'm not understanding your logic because I don't see all of the code, but something looks suspicious. It appears that you are reallocating the root node on every pass through the while loop. While the code may be working correctly on each pass, I believe it is trashing the previous pass and only creating the trie for the current word. If so, then only the last word will be in the dictionary. If this is true, you should either allocate the root node before the while loop or (less efficient) test to see if it exists before allocating it. I could be very wrong, but it should be investigated.

Also, it might be interesting to see what happens if foobar is the first word instead of the last word in your dictionary. Will only the last word remain, or will all 3 last words remain? I think it will still be only the last.

I haven't fully analyzed your code, but something else jumped out immediately. In function addchar(...), you call chr2idx(c); which returns the index value idx. Unfortunately, you don't do anything with that value. Whatever is in idx in addchar() is being used. I'm surprised it didn't give a compile error, unless idx is a global. Try fixing this and let us know if it works.

If this answers your question, please click on the check to accept and to remove this question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

  • Reallocating the root node within the while loop was the problem. Now it works :-) Yes idx is defined on a broader scope. Thank you very much!
    – Lex
    Commented Aug 7, 2015 at 20:03

You must log in to answer this question.

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