So this is what I'm getting when I run help50 valgrind:
/etc/profile.d/cli.sh: line 94: 538 Segmentation fault valgrind ./speller texts/cat.txt
Looks like your program is trying to access areas of memory that it isn't supposed to access. Did you try to change a character in a hardcoded string? Are you accessing an element of an array beyond the size of the array? Are you dereferencing a pointer that you haven't initialized? Are you dereferencing a pointer whose value is NULL? Are you dereferencing a pointer after you've freed it?
I can't figure out what I'm doing wrong and I would really appreciate your help.
This is line 94 of speller.c :
91 // Ignore words with numbers (like MS Word can)
92 else if (isdigit(c))
93 {
94 // Consume remainder of alphanumeric string
95 while ((c = fgetc(file)) != EOF && isalnum(c));
96
97 // Prepare for new word
98 index = 0;
99 }
This is my code (dictionary.c) :
// Implements a dictionary's functionality
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include "dictionary.h"
#define HASHTABLE_SIZE 1985
//word counter
int counter = 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 = 26;
// Hash table
node *table[N];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
int index = hash(word);
//Set to the head of the linked list
node* head = table[index];
if(head != NULL)
{
node* cursor = head;
while(cursor->next != NULL)
{
if(strcasecmp(cursor->word, word) == 0)
{
return true;
}
cursor = cursor->next;
}
}
return false;
}
//Hash table that I got from https://stackoverflow.com/questions/7666509/hash-function-for-string
// Hashes word to a number
unsigned long hash(const char *str)
{
unsigned long hash = 5381;
int c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
/*unsigned int hash(const char *word)
{
// TODO
return 0;
}*/
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
//Open dictionary for read
FILE *f = fopen(dictionary, "r");
//Check to see if dictionary is empty
if(f == NULL)
{
printf("Error opening the file.\n");
return false;
}
//array to store words
char words[LENGTH + 1];
//Read strings from the file
while(fscanf(f, "%s", words) != EOF)
{
//allocate enough memory to store a new node
node *n = malloc(sizeof(node));
//if it doesn't have enough memory to store a node
if(n == NULL)
{
unload();
return false;
}
//copy the word from the array to the node
strcpy(n->word, words);
//call hash function
int index = hash(words) % HASHTABLE_SIZE;
//insert the node in the hash table
n->next = table[index];
table[index] = n;
counter++;
}
//close the file
fclose(f);
//success
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
// TODO
return counter;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
for(int i = 0 ; i < HASHTABLE_SIZE ; i ++)
{
node* cursor = table[i];
node* tmp = cursor;
while(cursor != NULL)
{
cursor = cursor->next;
free(tmp);
tmp = cursor;
}
free(cursor);
}
return true;
}