I have a somewhat okay understanding on how linked lists worked since I've had some experience working with them in high school. However, I keep running into these pesky segfaults and I have no idea why. Some help would be appreciated. Also let me know if there are any logical errors I might have overlooked. Thanks.
bool load(const char *dictionary)
{
// TODO
FILE *file = fopen(dictionary, "r");
if (file==NULL) //if file could not be opened
{
fclose(file);
return false;
}
char word[LENGTH]; //initializing string
for (int i = 0; i < 1000; i++)
{
table[i] = NULL;
}
while (fscanf(file, "%s", word) != EOF) //copying each word from the dictionary file to the word
{
node *n = malloc(sizeof(node)); //allocating memory for a new node
if(n==NULL) //unable to allocate memory
{
unload();
return false;
}
strcpy(n->word, word); //stores the string into the location given as the first argument
unsigned long h = hash(word) % N;
if(table[h] == NULL) //if nothing is linked to the array yet
{
table[h]= n; //setting the first link
n->next=NULL;
Number_Of_Words++;
return true;
}
else if (table[h] != NULL) //if it is linked to something else already
{
n->next = table[h];
table[h] = n;
Number_Of_Words++;
return true;
}
}
return false;
}
Results:-
- WORDS MISSPELLED: 17314
- WORDS IN DICTIONARY: 1
- WORDS IN TEXT: 17756
- TIME IN load: 0.00
- TIME IN check: 0.01
- TIME IN size: 0.00
- TIME IN unload: 0.00
- TIME IN TOTAL: 0.01
[Re-edited code, I think load is the culprit here]