working on pset4 speller via trie. I AM AWARE OF THE CLANG BUG. According to one of the answers in the original question i removed the -fsanitize flag and ran valgrind() on it while using a small dictionary of :
do
dog
foo
fox
so valgrind gives this message:
HEAP SUMMARY:
==603== in use at exit: 0 bytes in 0 blocks
==603== total heap usage: 13 allocs, 13 frees, 12,112 bytes allocated
==603==
==603== All heap blocks were freed -- no leaks are possible
==603==
==603== For counts of detected and suppressed errors, rerun with: -v
==603== ERROR SUMMARY: 1468 errors from 11 contexts (suppressed: 0 from 0)
Is the last line ok??? Is the unload working fine ??? It says that there is no memory leak...
BUT when i ran valgrind with the default large dictionary .Probably after printing misspelled names the IDE seems to lose connection to server and i am prompted to the dashboard. I think there is something wrong with my unload, which is probably not showing when i use small dictionary. This is the unload which is working fine(in a sense that i am not getting segfault)
bool func_unload(node *unloader)
{
node *temp = unloader;
for(int i = 0; i < N; i++)
{
temp = unloader;
if(unloader -> children[i] != NULL)
{
func_unload(unloader -> children[i]);
}
}
free(unloader);
unloader = temp;
return true;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
func_unload(root);
return true;
}
Most importantly why is the IDE shutting down
EDIT: here is the load
while (fscanf(file, "%s", word) != EOF)
{
node *trav = root;
for(int i = 0, s = strlen(word); i < s; i++)
{
if (isalpha(word[i]))
{
index = tolower(word[i]) - 97;
}
else
{
index = 26;
}
if ((trav -> children[index]) == NULL)
{
(trav -> children[index]) = malloc(sizeof(node));
}
trav = trav -> children[index];
}
trav -> is_word = true;
}
// Close dictionary
fclose(file);
EDIT: So plz take a look at my size function. Sorry but i have to clear this concept in my mind.
unsigned int size(void)
{
node *trav = root;
int ctr = 0;
for (int i = 0; i < N; i++)
{
//Sizer is also a node pointer initialized to root previously
if (sizer -> children[i] == NULL)
{
continue;
}
else
{
//Store the current value of sizer
trav = sizer;
//Change sizer to point to its child
sizer = sizer -> children[i];
if ((sizer -> is_word) == true)
{
ctr ++;
}
// recursively call size again
int x = size();
ctr += x;
/*After adding the number all the words IN THE CHILDREN of this particular child, I want to point to the original `sizer`, so that i can move on to the next child and repeat the same for its chidren. To do that i need something to point back to original position*/
sizer = trav;
}
}
return ctr;
I think definitely here i need trav
pointer , because otherwise i am not getting the actual number of words. Here i am using trav
to explicitly go to the previous location, it is not being remembered as you saying.
temp
is totally unnecessary, you can remove it. Otherwise looks fine. If there are issues, they are probably inload
(e.g. due to not initialising all of your allocated nodes, setting children pointers toNULL
andis_word
tofalse
), or less likely incheck
(some people callfree
there, freeing nodes still in use). Doesvalgrind
really not list those 11 contexts? No idea about the IDE. – Blauelf Jul 16 '19 at 20:24calloc
instead ofmalloc
, it's about the same but overwrites the allocated memory with zeroes, which isNULL
for pointers andfalse
for booleans, exactly what we need. – Blauelf Jul 16 '19 at 21:26