For my Pset4 Speller, the unload() function is not working. I have tracked the problem to the free() function not working, but cannot get further than that. Could someone offer me any pointers please? (Also please not in hexadecimal:) Also, thank you Blauelf for the corrections. Any more would be really helpful.
Code:
//Implements a dictionary's functionality
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dictionary.h"
//Represents number of children for each node in a trie
#define N 27
//Initializes global variable for trie size
int counter = 0;
//Represents root of a trie
trie *root;
//Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
//Opens dictionary
FILE *file = fopen(dictionary, "r");
if (!file)
{
return false;
}
root = calloc(1, sizeof(trie));
trie *insert = root;
trie *insert2 = root;
//Allocates a pointer to insert a word
for (char c = fgetc(file); c != EOF; c = fgetc(file))
{
if (c == '\n')
{
insert->is_word = true;
counter++;
insert = root;
insert2 = root;
}
else
{
int indexed;
if (c == '\'')
{
indexed = 26;
}
else if (isalpha(c))
{
c = toupper(c);
indexed = c - 65;
}
else
{
return false;
}
insert2 = insert->next[indexed];
if (!insert2)
{
insert2 = calloc(1, sizeof(trie));
if (!insert2)
{
return false;
}
insert->next[indexed] = insert2;
insert = insert2;
}
else
{
insert = insert2;
}
}
}
return true;
}
//Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
return counter;
}
//Returns true if word is in dictionary else false
bool check(const char *word)
{
trie *search = root;
for (int i = 0, length = strlen(word); i <= length; i++)
{
int indexed;
//Indexes char if not newline, checks word ending if it is
if (word[i] != '\n')
{
if (word[i] == '\0')
{
if (search->is_word)
{
return true;
}
else
{
return false;
}
}
else
{
if (word[i] == '\'')
{
indexed = 26;
}
else if (isalpha(word[i]))
{
char c = toupper(word[i]);
indexed = c - 65;
}
else
{
return false;
}
trie *search2 = search->next[indexed];
if (!search2)
{
return false;
}
search = search2;
}
}
else
{
return false;
}
}
return true;
}
//Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
deload(root);
return true;
}
void deload(trie *removing)
{
for (int i = 0; i < N; i++)
{
if (removing->next[i] != NULL)
{
deload(removing->next[i]);
}
}
//Since all the other pointers point to NULL, you can free the pointer
free(removing);
}