I have been on this for literal days, there is always a seg fault no matter what i do things i have tried:
- debug 50: MULTIPLE TIMES, i fix the errors i find but end up with another one soon after, depending on the changes i make, the seg fault is at different places
- check50: all the faces are red and frownng at me
- valgrind shows no leaks
- looked at other peoples solutions on github, did not help
i must be doing something wrong, i just dont know what, i am just going to share the entirety of my code here, so dont look if you are still working on this:
// Implements a dictionary's functionality
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include "dictionary.h"
int load_count = 0;
// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
// TODO: Choose number of buckets in hash table
const unsigned int N = 65536;
// Hash table
node *table[N];
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
// run word thru the hash function to obtain hash code and store in variable
int index = hash(word);
// assign cursor to point at the same address the first node at index is pointing at
node *cursor = table[index];
// stop when there is no more nodes to traverse
while(cursor -> next != NULL)
{
// traverse linked list, comparing word with word in current node
if (strcasecmp(word, cursor -> word) == 0)
{
return true;
}
// move to the next node in the linked list
cursor = cursor -> next;
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
int sum = 0;
// stop at the end of the string
for(int i = 0; word[i] != '\0'; i++)
{
// sum the ASCII value of each of the chars in the string
sum += toupper(word[i]);
}
// return the hash code
return sum % N;
}
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
char buffer[LENGTH + 1];
FILE *input;
// open dictionary file
if ((input = fopen(dictionary, "r")) == NULL)
{
// close file if pointer returns NULL
fclose(input);
return false;
}
// read input from dictionary file into buffer
while(fscanf(input, "%s", buffer) != EOF)
{
// create node
node *n = malloc(sizeof(node));
if (n == NULL)
{
return false;
}
// copy string from buffer into node
strcpy(n -> word, buffer);
n -> next = NULL;
// run buffer thru hash function to obtain hash code
unsigned int hash_code = hash(buffer);
// if location at hash table is empty, assign current node as first node
if (table[hash_code] == NULL)
{
table[hash_code] = n;
}
// else insert node at the beginning of linked list
else
{
n -> next = table[hash_code];
table[hash_code] = n;
load_count++;
}
}
fclose(input);
return true;
}
// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
if (load_count > 0)
{
return load_count;
}
return 0;
}
// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
// create two temporary nodes
node *temp, *cursor = malloc(sizeof(node));
// loop thru entire array
for (int i = 0; i < N; i++)
{
// assign temporary node to point at the same address the first node at current index is pointing at
cursor = table[i];
// loop thru linked list
while(cursor -> next != NULL)
{
// assign both temporary nodes to point at the same node
temp = cursor;
// move one temporary node to the next node
cursor = cursor -> next;
// free the other temporary node
free(temp);
}
}
return true;
}