0

When I run help50 valgrind ./speller texts/lalaland/txt it returns

==1123== 
==1123== HEAP SUMMARY:
==1123==     in use at exit: 472 bytes in 1 blocks
==1123==   total heap usage: 143,096 allocs, 143,095 frees, 8,023,256 bytes allocated
==1123== 
==1123== 472 bytes in 1 blocks are still reachable in loss record 1 of 1
==1123==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==1123==    by 0x4A27AAD: __fopen_internal (iofopen.c:65)
==1123==    by 0x4A27AAD: fopen@@GLIBC_2.2.5 (iofopen.c:86)
==1123==    by 0x401ABE: load (dictionary.c:64)
==1123==    by 0x4012CE: main (speller.c:40)
==1123== 
==1123== LEAK SUMMARY:
==1123==    definitely lost: 0 bytes in 0 blocks
==1123==    indirectly lost: 0 bytes in 0 blocks
==1123==      possibly lost: 0 bytes in 0 blocks
==1123==    still reachable: 472 bytes in 1 blocks
==1123==         suppressed: 0 bytes in 0 blocks
==1123== 
==1123== For lists of detected and suppressed errors, rerun with: -s
==1123== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Asking for help...

Sorry, help50 does not yet know how to help with this!

But when I run check50 cs50/problems/2021/x/speller it returns

:) 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; see log for more information.

Why are valgrind and check50 telling different things?

1

There is a memory leak:

472 bytes in 1 blocks are still reachable in loss record 1 of 1
==1123==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==1123==    by 0x4A27AAD: __fopen_internal (iofopen.c:65)
==1123==    by 0x4A27AAD: fopen@@GLIBC_2.2.5 (iofopen.c:86)
==1123==    by 0x401ABE: load (dictionary.c:64)
==1123==    by 0x4012CE: main (speller.c:40)

When there are no leaks, valgrind gives the message:

All heap blocks were freed -- no leaks are possible

Since the leak points to iofopen, it's likely that line 64 in dictionary opens the dictionary file with fopen. The file must be closed to free that memory.

Anything valgrind complains about is a "valgrind error", whether it is a leak (ie allocated memory that is not freed) or an error (eg conditional jump or depends on unitialised value(s)).

You must log in to answer this question.

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