I am trying to fix the last error in my code pointed out by check 50. I think the problem is in my unload but can't say for sure. Here's my code. I know I have some basic functionalities wrong but everything is correct in check 50 except the last one. I just wanna fix that.
// Implements a dictionary's functionality
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <strings.h>
#include "dictionary.h"
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// Number of buckets in hash table
const unsigned int N = 26;
// Hash table
node *table[N];
int count;
// Returns true if word is in dictionary else false
bool check(const char *words)
{
int index = hash(words);
node *cursor = NULL;
cursor = table[index];
while (cursor != NULL)
{
if (strcasecmp(cursor->word, words) == 0)
{
return true;
}
else
{
cursor = cursor->next;
}
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
char letter = tolower(word[0]);
int index = letter - 'a';
return index;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
for (int i = 0; i < N; i++)
{
table[i] = NULL;
}
char *words = malloc(45 * sizeof(char *));
FILE *diction = fopen(dictionary, "r");
if (diction == NULL)
{
printf("Could not open file");
fclose(diction);
return false;
}
while (/*char *scan =*/ fscanf(diction, "%s", words) != EOF)
{
node *nod = malloc(sizeof(node));
if (nod == NULL)
{
printf("Error");
fclose(diction);
return false;
}
strcpy(nod->word, words);
int index = hash(nod->word);
node *tmp = table[index];
if (table[index] == NULL)
{
table[index] = nod;
}
else
{
while (tmp->next != NULL)
{
tmp = tmp->next;
}
tmp->next = nod;
}
count++;
}
fclose(diction);
free(words);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
return count;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
for (int i = 0; i < 27; i++)
{
node *cursor = table[i];
node *tmp = cursor;
while (cursor != NULL)
{
cursor = cursor->next;
free(tmp);
tmp = cursor;
}
}
return true;
}
Here is the check 50.
:( program is free of memory errors
valgrind tests failed; rerun with --log for more information.
running valgrind --show-leak-kinds=all --xml=yes --xml-file=/tmp/tmp880pw7za -- ./speller substring/dict substring/text...
checking for output "MISSPELLED WORDS\n\nca\ncats\ncaterpill\ncaterpillars\n\nWORDS MISSPELLED: 4\nWORDS IN DICTIONARY: 2\nWORDS IN TEXT: 6\n"...
checking that program exited with status 0...
checking for valgrind errors...
Conditional jump or move depends on uninitialised value(s): (file: dictionary.c, line: 84)
Conditional jump or move depends on uninitialised value(s): (file: dictionary.c, line: 33)
Conditional jump or move depends on uninitialised value(s): (file: dictionary.c, line: 110)