I have been trying to debug this for over 2 days now. Can't seem to locate the reason why there's a segmentation fault. Running this returns a bunch of misspelled words plus an error message -
speller: malloc.c:2372: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.
Aborted (core dumped)
Any help would be deeply appreciated guys. Can't seem to make much sense of the error. Here's the load function
bool load(const char* dictionary)
{
//Open Dictionary with a read only file
FILE* file = fopen(dictionary, "r");
if(file == NULL)
{
printf("Unable to open Dictionary file \n");
return false;
}
char wd[LENGTH+1];
//iterate through every word
while(!feof(file))
{
node* new_word = malloc(sizeof(node));
fscanf(file,"%s",wd);
count++;
strcpy(new_word->word, wd);
new_word->next = NULL;
//hash function - Getting the index
int index = hash_fun(new_word->word);
if (index >0)
{
if (hashtable[index] == NULL)
{
hashtable[index] = new_word;
}
else if (hashtable[index] != NULL)
{
node* ptr = hashtable[index]; //initialising the temp pointer
while(ptr->next != NULL) //taking the pointer to the end of the linked list
{
ptr = ptr->next;
}
ptr->next = new_word;
}
}
}
if(feof(file))
{
return true;
fclose(file);
}
return false;
fclose(file);
}
and here's the check function along with the global variables and hash_function
int count = 0;
typedef struct node
{
char word[LENGTH + 1];
struct node* next;
}node;
node* hashtable[26];
int hash_fun(char* key)
{
int n = tolower(key[0])-'a';
return n % 26;
}
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char* word)
{
char* string = malloc(sizeof(word));
strcpy(string,word);
int x = strlen(word);
for(int i = 0;i<x; i++)
{
string[i] = tolower(string[i]);
}
int index = hash_fun(string);
node* crawler = hashtable[index];
while(crawler != NULL)
{
if (strcmp(crawler->word,string) == 0)
{
return true;
}
crawler = crawler->next;
}
return false;
}
feof
returns non-zero after the end-of-file has been reached not whether the next thing to read is the end-of-file.