0

When I run speller I get a segmentation fault - I am pretty sure that the error is in this excerpt of my load function. I don't understand why; hashtable is an array of node pointers. Then new_word is also a node pointer. It points towards an unnamed node which has been allocated a space in memory with malloc(). Then the node pointed at by new_word has its word parameter changed with the arrow operator equivalent to (*new_word).word. Then either the hashtable[value] is a null pointer, in which case, the new_word pointer points to NULL and the pointer in hashtable points to new_word, either there already is something in the hashtable[value] and so, with a cursor, we find the first pointer pointing to NULL and set it to point to the new_word. Seems ok to me and yet... I get an error.

while(!feof(words)){
    node* new_word;
    new_word = malloc(sizeof(node));
    fscanf(words, "%s", new_word -> word);
    hash_value = hashit(new_word -> word);
    if (HASHTABLE[hash_value] == NULL){
        new_word -> next = NULL;
        HASHTABLE[hash_value] = new_word;
    }
    else {
        node* cursor = HASHTABLE[hash_value];
        while(cursor->next != NULL) {
            cursor = cursor->next;
        }
        new_word->next = NULL;
        cursor->next = new_word;
    }

}

I also included the check function just in case:

bool check(const char* word)
{
    signed int bucket;
    bucket = hashit(word);
    node* search = HASHTABLE[bucket];
    while(search -> next != NULL){
        if(strcmp(search -> word, word)) {
            return true;
        }
        search = search -> next;
    }
    return false;
}

Current code for check:

bool check(const char* word)
{
    char temp[strlen(word)];
    for(int j = 0; j < strlen(word); j++){
        temp[j] = islower(word[j]);
    }
    temp[strlen(word)] = '\0';
    signed int bucket;
    bucket = hashit(temp);
    node* search = HASHTABLE[bucket];
    if (HASHTABLE[bucket] == NULL){
        return false;
    }
    else {
        while(search -> next != NULL){
            if(strcmp(search -> word, temp) == 0) {
                return true;
            }
            else {
                return false;
            }
            search = search -> next;
        }  
    }
    return true;
}

1 Answer 1

0

It appears that your seg fault is in check(). If it is occurring after MISSPELLED WORDS is displayed, then this confirms it it not in load. It's difficult to know for sure with the limited amount of code that you included in the question.

Assuming the problem is in check, it looks like the problem is with the while loop:

while(search -> next != NULL){

What happens if search is null itself? If so, search->next doesn't exist. This causes a seg fault. You need a check for search == NULL.

There may be more issues, but they would be very difficult to diagnose with what is provided.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Hi, I fixed this bug, thank you very much. However my check still does not work... I put the current code in the edit. Would you be able to tell me what the issue is?
    – Gabe
    Commented Sep 6, 2016 at 0:26

You must log in to answer this question.

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