Ok...I'm doing pset5 and I'v already done the load function though I think there's something wrong with it.When I run speller it gives a seg fault in check function.Something to do with strcmp according to gdb.
After a looot of debugging, I'v found out that it crashes in the strcmp function and for some reason my second node of the first bucket..Um..first linked list in the array is this
"IN\000urgy\000\000ns\000\000m\000\000\000\000\000\000\000\000\000csilicovolcanoconiosis"
And here's my hash function, super simple I know, I'm gonna change it in the end.
int hashy(char* word){
int num = (int)(toupper(word[0]) - 65);
return num % 26;
}
And here's my load-
bool load(const char* dictionary)
{
FILE* dic = fopen(dictionary,"r");
//Make all the next pointers NULL
for(int j=0;j<26;j++){
letters[j] = malloc(sizeof(node));
letters[j]->next = NULL;
}
//Check if file could be opened
if(dic == NULL){
printf("File not found, I repeat File not found");
return false;
}
for(int i = 0;!feof(dic);i++){
//new node
node* new_node = malloc(sizeof(node));
//get the word and store it in the node
fscanf(dic,"%s",new_node->word);
//first time
if(letters[hashy(new_node->word)]->next == NULL){
//letters[hashy(new_node->word)]->word = new_node->word;
strcpy(letters[hashy(new_node->word)]->word,new_node->word);
letters[hashy(new_node->word)]->next = new_node;
}else{
node* crawler = letters[hashy(new_node->word)]->next;
while(!(crawler->next == NULL)){
crawler = crawler->next;
}
crawler->next = new_node;
}
free(new_node);
}
fclose(dic);
return true;
}
And finally the check function-
bool check(const char* word)
{
char* worda = malloc(46);
strcpy(worda,word);
//find the wanted bucket
int buc = hashy(worda);
//loop through word
for(int i = 0,n = strlen(worda);i < n;i++){
if(isupper(worda[i])){
tolower(worda[i]);
}
}
node* crawler = letters[buc];
while(crawler != NULL){
if(strcmp(crawler->word,worda) == 0){
free(crawler);
free(worda);
return true;
}
crawler = crawler->next;
}
free(crawler);
free(worda);
return false;
}
Oh and the worda dosent get converted to lowercase letters in the function. I'v declared the letters array and struct node globally.Oh and here's what gdb shows in the end-
67 crawler = crawler->next;
(gdb) n
69 }
(gdb)
59 while(crawler != NULL){
(gdb)
61 if(strcmp(crawler->word,worda) == 0){
(gdb)
Program received signal SIGSEGV, Segmentation fault.
__strcmp_sse4_2 () at ../sysdeps/i386/i686/multiarch/strcmp-sse4.S:217
217 movdqu (%edx), %xmm2
Please do tell me what I'm doing wrong.