Been scratching my head at this for a while now. I use FREE() only once in the program, and don't know why I get this error. Any help is appreciated :-)
// Implements a dictionary's functionality
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <string.h>
#include <strings.h>
#include "dictionary.h"
// Keep track of how many words are loaded into the dictionary
unsigned int word_count = 0;
// 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 = 10000;
// Hash table
node *table[N];
node *traveller;
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
unsigned int hashed = hash(word);
traveller = table[hashed];
while (traveller != NULL)
{
if (strcasecmp(traveller->word, word) == 0)
{
return true;
}
traveller->next = traveller->next;
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word) // CREDIT: Dan Berstein http://www.cse.yorku.ca/~oz/hash.html
{
int hash = 401;
int c;
while (*word != '\0')
{
hash = ((hash << 4) + (int)(*word)) %N;
word++;
}
return hash % N;
}
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
char word[LENGTH + 1];
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
printf("Couldn't open dictionary\n");
return false;
}
while (fscanf(file, "%s", word) != EOF)
{
node *p = malloc(sizeof(node)); // Maloc a new node for our hashed linked list
if (p == NULL) // Check that the memory allocation is not NULL
{
printf("Error allocating node");
return NULL;
}
strcpy(p->word, word); // Copy from the char buffer the string into the char field in the node
p->next = NULL; // This will be the last node in the list so it must point to NULL
word_count++; // Count how many strings read from file
int hashed = hash(word); // Call the hash function to return a deterministic int value
table[hashed] = p; // Using the hashed value as key to index into the root array of the list to point at the (initially) first node in the linked list
p->next = table[hashed]; // Point new node to the first (old) node in the list contained in the pointer array value
table[hashed] = p; // Point array pointer element to address of the new node
}
fclose(file);
return true;
}
// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
if (word_count > 0)
{
return word_count;
}
else
{
return 0;
}
}
// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
node *temp, *cursor; // Pointers to track/traverse/delete the linked list nodes
for (int i = 0; i <= N; i++)
{
cursor = table[i];
temp = cursor;
while (cursor != NULL)
{
cursor = cursor->next;
free(temp);
temp = cursor;
}
}
if (table[N - 1] == NULL)
{
return true;
}
else
{
return false;
}
}
Execution:
~/pset5/speller/ $ ./speller dictionaries/small texts/cat.txt
Error message:
MISSPELLED WORDS
A is not a free(): double free detected in tcache 2 Aborted