I tested my code on multiple dicitonaries/texts, it all works nicely, even at times faster than the staff's solution. When I run valgrind, I get no memory leaks but 14 errors, seemingly in Load, and that regardless of the dictionary I am loading.
This is my Load function
bool load(const char* dictionary)
{
FILE* fp = fopen (dictionary,"r");
if (fp == NULL)
{
printf("Could not open %s.\n", dictionary);
return 1;
}
//int index = 0; to count the number of words, necesary?
bool isNewWord = 1;
root = calloc(1,sizeof(node));
trav = root;
for (int c = fgetc(fp); c != EOF; c = fgetc(fp)) //Looping caracter by caracter inside the dictionary
{
if (isNewWord==1)
{
trav = root;
}
if (c != 10) //if you havent reached the line feed (code '10')
{
if (c == 39) //if word has an apostrophe
{
c = 26; //last index in the word, for apostrophe
}
else //otherwise put it in its nth number of letter in the alphabet spot
{
c = c - 97;
}
if (trav->children[c] == NULL)
{
node* NextLevel = calloc (1,sizeof(node));
NextLevel->isWord = 0;
trav->children[c] = NextLevel;
}
trav = trav->children[c];
isNewWord = 0;
}
else
{
trav->isWord = 1;
isNewWord = 1;
NumberOfWords ++;
}
}
free(fp);
return true;
}
and this is the Valgrind report I get
> ==13808== Invalid read of size 4
==13808== at 0x4EB302D: _IO_flush_all_lockp (genops.c:841)
==13808== by 0x4EB31B9: _IO_cleanup (genops.c:1013)
==13808== by 0x4E7320A: __run_exit_handlers (exit.c:95)
==13808== by 0x4E732A4: exit (exit.c:104)
==13808== by 0x4E58ECB: (below main) (libc-start.c:321)
==13808== Address 0x51fc100 is 192 bytes inside a block of size 568 free'd
==13808== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13808== by 0x4012F3: load (dictionary.c:118)
==13808== by 0x40087D: main (speller.c:45)
==13808==
==13808== Invalid read of size 8
==13808== at 0x4EB3120: _IO_flush_all_lockp (genops.c:841)
==13808== by 0x4EB31B9: _IO_cleanup (genops.c:1013)
==13808== by 0x4E7320A: __run_exit_handlers (exit.c:95)
==13808== by 0x4E732A4: exit (exit.c:104)
==13808== by 0x4E58ECB: (below main) (libc-start.c:321)
==13808== Address 0x51fc060 is 32 bytes inside a block of size 568 free'd
==13808== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13808== by 0x4012F3: load (dictionary.c:118)
==13808== by 0x40087D: main (speller.c:45)
==13808==
==13808== Invalid read of size 8
==13808== at 0x4EB3124: _IO_flush_all_lockp (genops.c:841)
==13808== by 0x4EB31B9: _IO_cleanup (genops.c:1013)
==13808== by 0x4E7320A: __run_exit_handlers (exit.c:95)
==13808== by 0x4E732A4: exit (exit.c:104)
==13808== by 0x4E58ECB: (below main) (libc-start.c:321)
==13808== Address 0x51fc068 is 40 bytes inside a block of size 568 free'd
==13808== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13808== by 0x4012F3: load (dictionary.c:118)
==13808== by 0x40087D: main (speller.c:45)
==13808==
==13808== Invalid read of size 8
==13808== at 0x4EB30CA: _IO_flush_all_lockp (genops.c:862)
==13808== by 0x4EB31B9: _IO_cleanup (genops.c:1013)
==13808== by 0x4E7320A: __run_exit_handlers (exit.c:95)
==13808== by 0x4E732A4: exit (exit.c:104)
==13808== by 0x4E58ECB: (below main) (libc-start.c:321)
==13808== Address 0x51fc0a8 is 104 bytes inside a block of size 568 free'd
==13808== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13808== by 0x4012F3: load (dictionary.c:118)
==13808== by 0x40087D: main (speller.c:45)
==13808==
==13808== Invalid read of size 4
==13808== at 0x4EB31D0: _IO_cleanup (genops.c:952)
==13808== by 0x4E7320A: __run_exit_handlers (exit.c:95)
==13808== by 0x4E732A4: exit (exit.c:104)
==13808== by 0x4E58ECB: (below main) (libc-start.c:321)
==13808== Address 0x51fc040 is 0 bytes inside a block of size 568 free'd
==13808== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13808== by 0x4012F3: load (dictionary.c:118)
==13808== by 0x40087D: main (speller.c:45)
==13808==
==13808== Invalid write of size 4
==13808== at 0x4EB3270: _IO_cleanup (genops.c:990)
==13808== by 0x4E7320A: __run_exit_handlers (exit.c:95)
==13808== by 0x4E732A4: exit (exit.c:104)
==13808== by 0x4E58ECB: (below main) (libc-start.c:321)
==13808== Address 0x51fc100 is 192 bytes inside a block of size 568 free'd
==13808== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13808== by 0x4012F3: load (dictionary.c:118)
==13808== by 0x40087D: main (speller.c:45)
==13808==
==13808== Invalid read of size 8
==13808== at 0x4EB327A: _IO_cleanup (genops.c:950)
==13808== by 0x4E7320A: __run_exit_handlers (exit.c:95)
==13808== by 0x4E732A4: exit (exit.c:104)
==13808== by 0x4E58ECB: (below main) (libc-start.c:321)
==13808== Address 0x51fc0a8 is 104 bytes inside a block of size 568 free'd
==13808== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13808== by 0x4012F3: load (dictionary.c:118)
==13808== by 0x40087D: main (speller.c:45)
==13808==
==13808== Invalid read of size 4
==13808== at 0x4EB302D: _IO_flush_all_lockp (genops.c:841)
==13808== by 0x4EB31B9: _IO_cleanup (genops.c:1013)
==13808== by 0x4F9C57A: __libc_freeres (in /lib/x86_64-linux-gnu/libc-2.19.so)
==13808== by 0x4A256BC: _vgnU_freeres (in /usr/lib/valgrind/vgpreload_core-amd64-linux.so)
==13808== by 0x4E7321A: __run_exit_handlers (exit.c:97)
==13808== by 0x4E732A4: exit (exit.c:104)
==13808== by 0x4E58ECB: (below main) (libc-start.c:321)
==13808== Address 0x51fc100 is 192 bytes inside a block of size 568 free'd
==13808== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13808== by 0x4012F3: load (dictionary.c:118)
==13808== by 0x40087D: main (speller.c:45)
==13808==
==13808== Invalid read of size 8
==13808== at 0x4EB3120: _IO_flush_all_lockp (genops.c:841)
==13808== by 0x4EB31B9: _IO_cleanup (genops.c:1013)
==13808== by 0x4F9C57A: __libc_freeres (in /lib/x86_64-linux-gnu/libc-2.19.so)
==13808== by 0x4A256BC: _vgnU_freeres (in /usr/lib/valgrind/vgpreload_core-amd64-linux.so)
==13808== by 0x4E7321A: __run_exit_handlers (exit.c:97)
==13808== by 0x4E732A4: exit (exit.c:104)
==13808== by 0x4E58ECB: (below main) (libc-start.c:321)
==13808== Address 0x51fc060 is 32 bytes inside a block of size 568 free'd
==13808== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13808== by 0x4012F3: load (dictionary.c:118)
==13808== by 0x40087D: main (speller.c:45)
==13808==
==13808== Invalid read of size 8
==13808== at 0x4EB3124: _IO_flush_all_lockp (genops.c:841)
==13808== by 0x4EB31B9: _IO_cleanup (genops.c:1013)
==13808== by 0x4F9C57A: __libc_freeres (in /lib/x86_64-linux-gnu/libc-2.19.so)
==13808== by 0x4A256BC: _vgnU_freeres (in /usr/lib/valgrind/vgpreload_core-amd64-linux.so)
==13808== by 0x4E7321A: __run_exit_handlers (exit.c:97)
==13808== by 0x4E732A4: exit (exit.c:104)
==13808== by 0x4E58ECB: (below main) (libc-start.c:321)
==13808== Address 0x51fc068 is 40 bytes inside a block of size 568 free'd
==13808== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13808== by 0x4012F3: load (dictionary.c:118)
==13808== by 0x40087D: main (speller.c:45)
==13808==
==13808== Invalid read of size 8
==13808== at 0x4EB30CA: _IO_flush_all_lockp (genops.c:862)
==13808== by 0x4EB31B9: _IO_cleanup (genops.c:1013)
==13808== by 0x4F9C57A: __libc_freeres (in /lib/x86_64-linux-gnu/libc-2.19.so)
==13808== by 0x4A256BC: _vgnU_freeres (in /usr/lib/valgrind/vgpreload_core-amd64-linux.so)
==13808== by 0x4E7321A: __run_exit_handlers (exit.c:97)
==13808== by 0x4E732A4: exit (exit.c:104)
==13808== by 0x4E58ECB: (below main) (libc-start.c:321)
==13808== Address 0x51fc0a8 is 104 bytes inside a block of size 568 free'd
==13808== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13808== by 0x4012F3: load (dictionary.c:118)
==13808== by 0x40087D: main (speller.c:45)
==13808==
==13808== Invalid read of size 4
==13808== at 0x4EB31D0: _IO_cleanup (genops.c:952)
==13808== by 0x4F9C57A: __libc_freeres (in /lib/x86_64-linux-gnu/libc-2.19.so)
==13808== by 0x4A256BC: _vgnU_freeres (in /usr/lib/valgrind/vgpreload_core-amd64-linux.so)
==13808== by 0x4E7321A: __run_exit_handlers (exit.c:97)
==13808== by 0x4E732A4: exit (exit.c:104)
==13808== by 0x4E58ECB: (below main) (libc-start.c:321)
==13808== Address 0x51fc040 is 0 bytes inside a block of size 568 free'd
==13808== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13808== by 0x4012F3: load (dictionary.c:118)
==13808== by 0x40087D: main (speller.c:45)
==13808==
==13808== Invalid write of size 4
==13808== at 0x4EB3270: _IO_cleanup (genops.c:990)
==13808== by 0x4F9C57A: __libc_freeres (in /lib/x86_64-linux-gnu/libc-2.19.so)
==13808== by 0x4A256BC: _vgnU_freeres (in /usr/lib/valgrind/vgpreload_core-amd64-linux.so)
==13808== by 0x4E7321A: __run_exit_handlers (exit.c:97)
==13808== by 0x4E732A4: exit (exit.c:104)
==13808== by 0x4E58ECB: (below main) (libc-start.c:321)
==13808== Address 0x51fc100 is 192 bytes inside a block of size 568 free'd
==13808== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13808== by 0x4012F3: load (dictionary.c:118)
==13808== by 0x40087D: main (speller.c:45)
==13808==
==13808== Invalid read of size 8
==13808== at 0x4EB327A: _IO_cleanup (genops.c:950)
==13808== by 0x4F9C57A: __libc_freeres (in /lib/x86_64-linux-gnu/libc-2.19.so)
==13808== by 0x4A256BC: _vgnU_freeres (in /usr/lib/valgrind/vgpreload_core-amd64-linux.so)
==13808== by 0x4E7321A: __run_exit_handlers (exit.c:97)
==13808== by 0x4E732A4: exit (exit.c:104)
==13808== by 0x4E58ECB: (below main) (libc-start.c:321)
==13808== Address 0x51fc0a8 is 104 bytes inside a block of size 568 free'd
==13808== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13808== by 0x4012F3: load (dictionary.c:118)
==13808== by 0x40087D: main (speller.c:45)
==13808==
==13808==
==13808== HEAP SUMMARY:
==13808== in use at exit: 0 bytes in 0 blocks
==13808== total heap usage: 11 allocs, 11 frees, 3,152 bytes allocated
==13808==
==13808== All heap blocks were freed -- no leaks are possible
==13808==
==13808== For counts of detected and suppressed errors, rerun with: -v
==13808== ERROR SUMMARY: 14 errors from 14 contexts (suppressed: 0 from 0)
Any help really appreciated!