This is what valgrind is giving.
==7194== Invalid read of size 1
==7194== at 0x4C2E0F4: strlen (in /usr/lib/valgrind/vgpreload_memcheck- amd64-linux.so)
==7194== by 0x42278F: insert (tries.c:34)
==7194== by 0x422393: load (dictionary.c:32)
==7194== by 0x4208F2: main (speller.c:41)
==7194== Address 0xadf5fcd is 0 bytes after a block of size 45 alloc'd
==7194== at 0x4C2CC70: calloc (in /usr/lib/valgrind/vgpreload_memcheck- amd64-linux.so)
==7194== by 0x4223FE: load (dictionary.c:36)
==7194== by 0x4208F2: main (speller.c:41)
The dictionary.c:36 refers to "word =calloc(LENGTH,sizeof(char));" in following piece of code.
bool load(const char *dictionary)
{
my_trie = getNode();
char* word =calloc(LENGTH,sizeof(char));
int i=0;
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
printf("Could not open %s.\n", dictionary);
return false;
}
for (int c = fgetc(file); c != EOF; c = fgetc(file)){
if(c == '\n'){
insert(my_trie, word);
size_of_dictionary++;
i=0;
free(word);
word =calloc(LENGTH,sizeof(char)); // This is dictionary.c:36
}
else{
*(word+i)=c;
i++;
}
}
free(word);
return true;
}