1

I've update the code somewhat. Mostly fixing my hash function so it will run properly with non alphabetical words.

Three hours into this though and I still can't seem to figure it out.

I know now it halts at this line.

node->next = table[index]; //This line specifically according to the debugger
unsigned int hash(const char *word)
{
    int sum;
    int p;
    for(int i = 0; i < strlen(word); i++) {
        if(isalpha(word[i]) == 0)
        {
            p = 96;
        }

        else 
            p = tolower(word[i]);

        int cnv = p - 96;
        sum = sum + cnv;
        p = 0;
    }
    return sum;
}



// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // TODO
    // Open Dictionary File
    FILE *dict = fopen(dictionary, "r");
    if (dict == NULL)
    {
        printf("Could not open file");
        return false;
    }
    
    char buffer[LENGTH+1];
    
    while(fscanf(dict, "%s" , buffer) != EOF) {

        // Malloc for a new node
        node *node = malloc(sizeof(node));

        if(node == NULL) {
            printf("Could not allocate memory for *node");
            return false;
        }
                    
        sizeD++;
        // Copy the contents of buffer into the new node
        strcpy(node->word, buffer);

        // Get the hash value of the node
        int index = hash(node->word);

        // If the pointer at that index is empty, point it to the node (if its the first of its kind)      
        node->next = table[index];
        table[index] = node;              
    }

    fclose(dict);
    return true;
}

And since you are already here. For node->next = table[index]; why isn't it written as node->next = table[index]->next. Shouldn't I copy the value of a pointer? I don't understand how this is supposed to work if it's just pointing to the table[index] rather than what the table[index] is pointing at.

Any other issues you see with the code please feel free to bring up as I am trying to learn what else I am doing wrong.

Thank you!

1
  • p = 0 in hash() is unnecessary
    – C--
    Apr 27, 2021 at 5:21

1 Answer 1

1

Actually, your load() function is absolutely fine. The problem is your hash() unction. Your not normalising (%) the hash value by table length. which is why node->next = table[index]results in seg fault, because index > table length

4
  • That's kind of confusing me. Because my hash table takes the asci value of each individual letter, adds them all up and outputs the sum as the index value. But I have set my table to have 1170 values to account for the largest possible number (z = 26 x 45(maximum total characters)). So why would I need to normalize the hash value if I know the hash value can never surpass the table length? Or theoretically shouldn't be able to.
    – Multiplify
    Apr 27, 2021 at 13:09
  • @Multiplify, you should have specified that before. That being said sum should be set to zero. If not it will start with a garbage value which perhaps might be greater than 1170.
    – C--
    Apr 27, 2021 at 14:10
  • Thanks for your time. I went ahead and initialized sum to 0 like you said and it seems to have cleared it up for me. I perhaps was confused in that global variables are automatically initialized to NULL, where as local variables may be set to a garbage value.
    – Multiplify
    Apr 27, 2021 at 14:54
  • @Multiplify Yes globals are set to NULL, 0 depending on data-type. Whereas local variables are set to garbage random values. That being said you can override this behavior by setting those variables with your preferred value using assignment operator =
    – C--
    Apr 27, 2021 at 15:02

You must log in to answer this question.

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