Skip to main content
added 773 characters in body
Source Link
Gabe
  • 7
  • 3

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;
}

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;
}
Source Link
Gabe
  • 7
  • 3

PSET 5, Segfault in load

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;
}