0

My speller works properly and passes check50, but the valgrind report is still less than ideal.

I believe the problem is somewhere in my check() function, so I've attached a link to that section of code: (removed)

And here is my unload() as well: (removed)

==3072== HEAP SUMMARY:
==3072==     in use at exit: 20,832 bytes in 372 blocks
==3072==   total heap usage: 143,466 allocs, 143,094 frees, 8,035,120 bytes allocated

==3072== 20,832 bytes in 372 blocks are definitely lost in loss record 1 of 1
==3072==    at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3072==    by 0x40123E: check (dictionary.c:51)

==3072== LEAK SUMMARY:
==3072==    definitely lost: 20,832 bytes in 372 blocks
2

The info from valgrind is consistent with your estimation that the problem is in check. Since the only thing allocated in your check function is cursor it stands to reason that is the source of the leak. But, you may say, I am freeing cursor in unload()! Quite true, to the eyes. However, that is only freeing the memory you allocated to hashtable. The memory you allocated to the pointer called cursor in check() is never freed. But wait, there's more! Perhaps you shouldn't allocate new memory to cursor at all. It is a pointer that is pointing to a chunk of memory (hashtable[index]) that has (presumably) already been allocated on the stack.

7
  • So do I need to place a free(cursor) somewhere within check? I've tried this in a couple of places, but it then throws off the functionality of the program. – klmo8 Mar 29 '16 at 17:08
  • That's what I think; I've only read the code, not run it of course. What do you mean "throws off the functionality?". – DinoCoderSaurus Mar 29 '16 at 17:19
  • 1
    Or maybe you don't need to malloc at all...It's a pointer that you are setting to an already allocated chunk of memory.....(again, just reading code) – DinoCoderSaurus Mar 29 '16 at 17:26
  • Well, if I attempt to free(cursor) right before return'ing "true", then the program finds many more misspelled words than it should and no longer passes check50. – klmo8 Mar 29 '16 at 17:28
  • 1
    You declared hashtable somewhere, something like node* hashtable[27]. That allocated space for 27 node pointers on the stack. When you cursor = hashtable[index]; in check, you are setting cursor to point to that chunk of (previously allocated) memory. Is that helpful? (I hope :) – DinoCoderSaurus Mar 29 '16 at 17:53

You must log in to answer this question.

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