I need to fix the memory leak error for me to complete pset5. When running my code, I get:

:) dictionary.c exists
:) speller compiles
:) handles most basic words properly
:) handles min length (1-char) words
:) handles max length (45-char) words
:) handles words with apostrophes properly
:) spell-checking is case-insensitive
:) handles substrings properly
:( program is free of memory errors
    valgrind tests failed; rerun with --log for more information.

Valgrind says that the error is caused by line 90 node *n = malloc(sizeof(node))

running valgrind --show-leak-kinds=all --xml=yes --xml-file=/tmp/tmpp6cuc8aw -- ./speller substring/dict substring/text...
checking for output "MISSPELLED WORDS\n\nca\ncats\ncaterpill\ncaterpillars\n\nWORDS MISSPELLED: 4\nWORDS IN DICTIONARY: 2\nWORDS IN TEXT: 6\n"...
checking that program exited with status 0...
checking for valgrind errors...
112 bytes in 2 blocks are still reachable in loss record 1 of 1: (file: dictionary.c, line: 90) 

What I don't get is how would that cause an error. I've been stuck on this problem for quite a while now, and I've browsed for similar questions on this site, but I've had no luck so far. Any output or help is kindly appreciated

The code:

<Removed 03/07/2021 for academic honesty>

2 Answers 2


The problem is that the tree isn't being freed. You could add a printf statement inside the unload function's loop to show it.

The cause is interesting, it leads to unpredictable results in a full scale run. Sometimes it will work, sometimes it might not.

I'll give you a big hint. Have you checked the hash values being generated? Also, what is the ASCII Decimal value of 'a' vs. the hex value? ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • I'm kinda surprised that my code worked for the other scenarios though
    – Nobody
    Commented Mar 6, 2021 at 16:14
  • That's the thing. Using array indices that are outside of range can work or not work. It won't always throw an error. Results are unpredictable.
    – Cliff B
    Commented Mar 6, 2021 at 16:16

I just found a subtle error in the check function. The allocated size for the string is wrong. It's should've been word_copy[n + 1] instead of word_copy[LENGTH]. Thank you for your input though. I'm actually surprised that my program worked in the other scenarios.

  • Good catch on that.
    – Cliff B
    Commented Mar 6, 2021 at 16:16

You must log in to answer this question.

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