0

I'm really struggling with this one! I've just about got through to the last section I have to complete and I have two issues that I know of (I can't get 'check' to work so I don't know if 'load' is working properly really - even though 'size' is working fine so I'm hoping!) The first problem is that I couldn't pass the char 'word' provided by speller into my hash function because it is a const char * and my function needs a char * (my error said "passing const char * to parameter of type 'char *' discards qualifiers")?

So, I just duplicated the hash function (djb) & changed the value to a const char * thinking that would work - now I'm getting a segfault... So how do I pass word into my hash function to get the array location? The walkthrough didn't mention this issue!!

I'm also getting valgrind errors saying I have an uninitialised value at line 174...I've tried to make sure 'value' is initialised even though I don't know if that's what was needed but it still says the same - I can't see what else I can do with 'hashtable' so I'm stuck!!!

I would really appreciate any pointers....(LOL!) thank you!! :-)

The code is here: http://pastebin.com/Rm5XTAvL

0

The compiler wanting const char* might indicate you tried to test the hash function using a string constant.

Not sure what your test for spaces is meant to do, there should not be spaces in a word. Maybe strlen is a way to go?

A hash table does not have a head. The head of the linked list is already stored in hashtable[loc % len] (the %len is for mapping the integer to range 0..len-1, you used it in load, but not in check).

load should return false on failing to load the dict (e.g. file could not be opened), and true when being successful. You return 2 at some place.

You might not want to pass LENGTH to the hash function. Maybe use strlen(word) instead.

Inside the load loop, you create two nodes (two nodes for one word?). You then either put both as a new linked list in the bin (again, two nodes for one word?), or create a linked list the other way around and forget about both nodes (that never happens, as the condition is always false), or forget both nodes in memory leak space without doing anything.

There is no use for a head node, and the whole thing

//check if the index given points to an empty 'bucket' and if so set a head pointer at hashtable[value] pointing to the first node containing the word.
if (hashtable[value] == NULL)
    {
    hashtable[value] = head;
    head->next = new_node;
    }
//or add the node containing the word as the first word after the existing head node
else if  
    (hashtable[value] == head)    
        {
        new_node->next = head;
        head = new_node;
        }

could be just

    new_node->next = hashtable[value];
    hashtable[value] = new_node;

Also, I don't get your final if (new_node == NULL).

In unload, you should use len, not sizeof(hashtable), the latter is the former, but times a pointer's length in bytes. Also, there is no last node in the list to be freed. Remember, you loop until cursor == NULL, so you then call free(NULL);.

14
  • Hi again Blauelf! Thank you so much for giving me so much to work on!! I'm going to work my way through your points now, but I wanted to ask about head...I don't think I'm understanding it. I malloced two nodes for one word in case it was the first word in the linked list at the location given by the hash function - I should have made sure that was only in that case because obv if there's
    – CallyB
    Feb 14 '17 at 12:33
  • There is no need for a head node. You already have a head, the pointer in the hash table.
    – Blauelf
    Feb 14 '17 at 12:36
  • sorry - pressed return b4 i finished - this shd come after 'hash function': - because I thought I had to make a head at the beginning of each list (without adding a value, just a pointer, which is why I only copied the word into the new_word node, not into the head node. I thought that then I had to put a head node in the hash table as the first node in the linked list, and make it point to new_node with the word in it. )
    – CallyB
    Feb 14 '17 at 12:40
  • It was an error that I made the head node even if there was already something at hashtable[value] because then I should just insert the new_node after the existing head node. That's how I understood it from Zamyla's walk through because she has a head node at the beginning of each linked list - Did I get that wrong? And maybe if so that's why the memory problem is occuring - maybe it doesn't free the head node...?? Anyway, I'll look at your other points now - thank you again..!
    – CallyB
    Feb 14 '17 at 12:40
  • Sorry - I don't understand - does that mean that by making hashtable[50] I then have 50 head nodes automatically? And they point to the first word? But Zamyla uses the var 'head' quite a lot - how does that work if there isn't a 'head' var declared. Sorry if it's obvious - I've struggled to understand this one!!!!
    – CallyB
    Feb 14 '17 at 12:42

You must log in to answer this question.

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