0

The code is working for loading large dictionaries most of the times but when i am trying to load a small dictionary i get the message that dictionary couldn't be loaded, while after that when i am trying to load large dictionary i get segmentation fault. I have declared node *head=NULL; as a global variable, so i can use it for my search function. Below i am giving you the code. If anyone can look at it and tell me why i am getting this message it will be great! Thank you!!

node *head=NULL; //I have declare this as a global variable.

bool load(const char* dictionary) {

FILE *fp=fopen(dictionary, "r");

if (fp==NULL)
{
    printf("File dictionary could not be opened.\n");
    return false; 
}

while (!feof(fp))
{
    node *new_node=malloc(sizeof(node));
    fscanf(fp, "%s", new_node->word);
    int h=hash_function(new_node->word, strlen(new_node->word));

    if (hash_table[h]==NULL)
    {
        hash_table[h]=new_node;
        head=hash_table[h];
        head->next=NULL;
    }
    else if (hash_table[h]!=NULL)
    {
        new_node->next=head->next;
        head->next=new_node;
    }
}

if(feof(fp))
{
    return true;
    fclose(fp); 
}

return false;
fclose(fp);

}

hash function:

int hash_function(const char *key, int M) {

unsigned long h;

const char *us;

us = (const char *) key;

h=0;

while (*us !=0) { h=(h * BASE + *us)%M; us++; }

return h;

}

1
  • did you try using valgrind and gdb to figure out which line exactly causes the segfault? – kzidane Jan 23 '16 at 8:05
0

I plugged the code that you posted into mine, making a few modifications to let them work together and it seems to work without seg faults. I'm not saying it works correctly, but no seg faults. The other problems I'm seeing are probably due to incompatibilities between my code and yours. Having said that, I would want to see your hash function. I would confirm the hash values generated by it, and look at how they're used in load and again in check. The two parameters make me suspicious of the hash function, but your problems could lie anywhere. You could also have an infinite loop that is using up all your memory until the lack of space causes a seg fault.

"Could not open dictionary" is more likely due to the file not being where the program expected it, or to permissions rather than a coding problem.

Where in the code is the seg fault being generated? Does it happen during load or check, or somewhere else?

THis may get you going, but that's the best I can do with the information given.

3
  • Thanks so much Cliff. Here is the hash function and i also added it up. The segmentation error comes when i am trying to load the dictionary and it is due to an error in load. Check i haven't finished it yet to be honest with you – dean.d Jan 22 '16 at 21:17
  • int hash_function(const char *key, int M) { unsigned long h; const char *us; us = (const char *) key; h=0; while (*us !=0) { h=(h * BASE + *us)%M; us++; } return h; } – dean.d Jan 22 '16 at 21:18
  • Thank you all for your help! I couldn't finger out where exactly the problem was so i deleted and rewrote the entire load function. It should have been a small error somewhere because after that the function is loading without any problem. Still can't finger out what was wrong before but thanks for jumping in – dean.d Jan 23 '16 at 21:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .