# PSET5-Speller: Unload? When is it “true”?

Im working on the unload function and I had fortunately written a "print" function that printed every member in the hash table. Writing this I was able to use it for the unload and size function.

however....im really unsure when I should be returning true on the unload function. How would I know that the memory is free'd as it is without running valgrind?

/**
* Unloads dictionary from memory. Returns true if successful else false.
*/
{
node *ptr = NULL;
for(int i = 0; i < HASHTABLESIZE; i++)
{
if(hashtable[i] == NULL)
{
i++;
}
else
{
ptr = hashtable[i];
while(ptr->next != NULL)
{
node *temp = ptr;
ptr=ptr->next;
free(temp);
}
}
}
return false;
}


Also size for reference (hopefully it's remotely correct):

/**
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
unsigned int size(void)
{
node *ptr = NULL;
int counter = 0;
for(int i = 0; i < HASHTABLESIZE; i++)
{
if(hashtable[i] == NULL)
{
i++;
}
else
{
ptr = hashtable[i];
while(ptr->next != NULL)
{
counter++;
ptr=ptr->next;
}
}
}
return counter;
if(counter == 0)
{
return 0;
}
}


Sure...if it goes through the entire loop it would be correct I would think? I suppose at the end I could go through every loop and make sure I see nothing but Null pointers on the array at the end?

Good question! It's sort of a case where you almost have to take it on faith that the tree has been freed.

To free the tree (or a trie), you have to walk the entire tree and free each element as you go, which you have done (assuming it is working correctly, which appears likely to be the case.) If you are properly walking the tree, then when it gets to the end, it should simply return true. Your code returns false at that point. ;-)

Now, the question is how thorough you want to be (or how mission critical it is, or how paranoid you are.) You could simply trust that the code is doing it's job - many people do exactly that. Or, you could add code to check whether the free command fails and to return false if it does (or take other actions), or implement more code to check more thoroughly that there is no failure or error in the unload.

For this assignment, some practical executions followed by valgrind checks that show no lost memory seems to be the standard. If this were for a practical application, personally, I would add code to check for successful execution of the free commands and leave it at that.

So, how paranoid are you? ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

• Mine is being done in a hash table not a tree, would it still be the same "assumption" – msmith1114 Feb 21 '17 at 2:26
• Yes, it would. In this case, it's a hash table where each element points to a linked list. You would still need to "walk" all of the linked lists and delete/free as you go. – Cliff B Feb 21 '17 at 2:28
• How exactly do you check that "free" works....since it's free'd haha? I know we can check Malloc to make sure it doesn't return a null but Im not sure how you would make sure "free" works – msmith1114 Feb 21 '17 at 2:53
• Another good question. ;-) I should have checked the documentation before assuming that free() returned a value, which it does not. oops. my bad. ;-) – Cliff B Feb 21 '17 at 4:49
• I guess in this case what you said still applies....sort of just have to trust it works if im able to get through the loops – msmith1114 Feb 21 '17 at 14:53