so I don't know what I should do with my unload function to make it work properly? Because valgrind isn't happy at all and it's giving 1000000 errors (literally). The load function works perfectly (I added it for referencing in my code below) but unload is wonky as hell.
Thanks in advance for the help!
Kind Regards
PrimeBeat
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
// load the dictionary file
FILE *dict_file = fopen(dictionary, "r");
if (dict_file == NULL)
{
return false;
}
// temporary storage for each word
char buffer[LENGTH + 1];
// read the individual strings from the dictionary file
while (fscanf(dict_file, "%s", buffer) != EOF)
{
// allocate memory for a new node (create a node)
node *new_node = malloc(sizeof(node));
if (new_node == NULL)
{
return false;
}
// copy the word into the newly created node
strcpy(new_node->word, buffer);
new_node->next = NULL;
if (table[hash(buffer)] == NULL)
{
table[hash(buffer)] = malloc(sizeof(node));
table[hash(buffer)] = new_node;
}
else
{
new_node->next = table[hash(buffer)];
table[hash(buffer)] = new_node;
}
size_of_dict++;
}
// free the dictionary file
fclose(dict_file);
return true;
}
// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
return size_of_dict;
}
// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
//loop through the hash table
for (int i = 0; i < N; i++)
{
struct node *current_table = table[i];
while (current_table != NULL)
{
struct node *cursor = table[i];
struct node *tmp = cursor;
cursor = cursor->next;
free(tmp);
}
free(current_table);
free(table[i]);
}
return true;
}