I have been trying to solve this for many days to no avail. It seems other many people have had similar problems with this pset, but when reading their questions, I had difficulty understanding what exactly went wrong with their code.

My valgrind output gives me this:

==10008== HEAP SUMMARY:
==10008==     in use at exit: 3,865,120 bytes in 17,255 blocks
==10008==   total heap usage: 367,084 allocs, 349,829 frees, 82,227,504 bytes allocated
==10008== 3,865,120 (1,914,304 direct, 1,950,816 indirect) bytes in 8,546 blocks are definitely lost in loss record 2 of 2
==10008==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10008==    by 0x4012BB: insert (dictionary.c:146)
==10008==    by 0x4011E3: load (dictionary.c:93)
==10008==    by 0x40090D: main (speller.c:40)
==10008== LEAK SUMMARY:
==10008==    definitely lost: 1,914,304 bytes in 8,546 blocks
==10008==    indirectly lost: 1,950,816 bytes in 8,709 blocks
==10008==      possibly lost: 0 bytes in 0 blocks
==10008==    still reachable: 0 bytes in 0 blocks
==10008==         suppressed: 0 bytes in 0 blocks

My program's outputs are correct--the only problem is the memory leak as far as I can tell. I've uploaded dictionary.c to Pastebin here: https://pastebin.com/JYNw45C1

Please note that some of the line numbers given by valgrind may be slightly off as I have deleted some variables I used for my own testing.

The majority of the work in load() is being done in the insert() function, which takes a char array as input and puts it character by character into the trie. Meanwhile, the majority of the work in unload() is being done by makeEmpty(), which is a recursive function that frees a node and all of its chilren. Please let me know if there is anything else I can clarify.

Thank you!


OH MY GOODNESS, I am an absolute idiot. I forgot that the array size I needed to iterate over is 27 instead of 26, since speller should recognize all the letters of the alphabet AND apostrophes. So I added a +1 to my for loop condition and the program works perfectly now.

Please believe me when I say I genuinely tried to solve this for a very long time on my own before resorting to asking for help....

Thanks for reading, and hopefully this helps someone in the future.

You must log in to answer this question.

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