First of all, i need to say that i am just trying to check my code working or not. For the following code, i used a so-called small dictionary which only includes the words "caterpillar" and "cat". My goal is to print the words cat and caterpillar respectively so that i ll check my hashtable[k] is working good. For testing purpose, i used the simplest hash_function as below. However, code prints only caterpillar twice. Please help me! I cant understand why!

int hash_func(char* key)
int hash = toupper(key[0]) - 'A';
return hash % 500;

bool load(const char* dictionary){
  node* new_node = malloc(sizeof(node));
  node* curr;
  int k;
    for(int i = 0; i < 26; i++){
    hashtable[i] = NULL;
  FILE* file = fopen(dictionary, "r");

  if (dictionary == NULL){
    printf("could not open the file\n");
    return false;

 while(fscanf(file, "%s", buffer -> word) == 1)
node* new_node = malloc(sizeof(node));
k = hash_func(buffer -> word);
new_node = buffer;

 if(hashtable[k] == NULL){
    new_node -> next = NULL;
    hashtable[k] = new_node;
    curr = hashtable[k];
    prev = curr;
    prev -> next = new_node;
    hashtable[k] = prev;

printf("%s\n", curr -> word);
printf("%s\n", prev-> next -> word);

return true;

} WHAT I SEE IN GDB!!! i just added the gdb part of the code, but i really did not understand what is going on. For the first iteration, all was normal, curr = cat and hashtable[k] = new_node and also is same " cat". However, after the second iterration, suddenly new_node appears as caterpillar??? @kareem (gdb) p *new_node $13 = {word = "cat", '\000' , next = 0x0} (gdb) n 109 hashtable[k] = new_node; (gdb) n 110 curr = hashtable[k]; (gdb) p *hashtable[k] $14 = {word = "cat", '\000' , next = 0x0} (gdb) p *curr $15 = { word = '\000' , "\340.\000\000\370\033", '\000' , "\256\001", '\000' , next = 0x0} (gdb) n 112 } (gdb) p curr $16 = {word = "cat", '\000' , next = 0x0} (gdb) n 124 } (gdb) n 97 node new_node = malloc(sizeof(node)); (gdb) p *new_node $17 = {word = "caterpillar", '\000' , next = 0x0} (gdb)

  • Why are you setting new_node->next to NULL after you assign it to hashtable[k]? What do you think this does? – kzidane Mar 30 '15 at 17:47
  • i thought that it ends the list. However, now i think that it needs to end the list after "caterpillar". Anyway, still not sure about why twice "caterpillar" i am seeing on the print screen? – mech10 Mar 31 '15 at 21:13
  • I don't think you should see "caterpillar" twice. It appears that you're trying to insert each new node at the beginning of the list by assigning the current list to new_node->next then assigning new_node to hashtable[k]. However, even after these assignments, new_node and hashtable[k] point to the same thing. Assigning NULL to new_node->next is the same as assigning hashtable[k]->next to NULL and that makes you lose reference to the tail of the list. Also you're allocating and using a single chunk of memory, so basically, when you read "caterpillar", you overwrite "cat". – kzidane Mar 31 '15 at 21:43
  • could you please give an example how not to overwrite cat because i tried all the variations still i could not find why. – mech10 Apr 1 '15 at 12:28
  • reddit.com/r/cs50/comments/2czhq6/… if i use the char array, i think i dont need malloc new_node after while loop. Am i right? – mech10 Apr 1 '15 at 17:03

You must log in to answer this question.

Browse other questions tagged .