I'm doing pset5 and there's something wrong with my load function.I'm getting some error which I think means I'm freeing memory twice but I cant understand what I'm doing wrong.

bool load(const char* dictionary)

    FILE* dic = fopen(dictionary,"r");

    //Make all the next pointers NULL
    for(int j=0;j<26;j++){
     letters[j]= NULL;

    //Check if file could be opened
    if(dic == NULL){

      printf("File not found, I repeat File not found");
      return false;


    for(int i = 0;!feof(dic);i++){

      //new node
      node* new_node = malloc(sizeof(node));

      //get the word and store it in the node

      //first time
      if(letters[hashy(new_node->word)]== NULL){

        letters[hashy(new_node->word)] = new_node;


        node* crawler = malloc(sizeof(node));

        crawler = letters[hashy(new_node->word)];

        while(!(crawler->next == NULL)){

         crawler = crawler->next;


        crawler->next = new_node;





return true;


This is the error:-

*** Error in `./speller': double free or corruption (fasttop): 0x08430170 ***
======= Backtrace: =========
======= Memory map: ========
08048000-0804a000 r-xp 00000000 fd:01 145267     /home/jharvard/cs50/pset5/speller
0804a000-0804b000 r--p 00001000 fd:01 145267     /home/jharvard/cs50/pset5/speller
0804b000-0804c000 rw-p 00002000 fd:01 145267     /home/jharvard/cs50/pset5/speller
08430000-08451000 rw-p 00000000 00:00 0          [heap]
46bba000-46bd9000 r-xp 00000000 fd:01 401475     /usr/lib/ld-2.17.so
46bd9000-46bda000 r--p 0001e000 fd:01 401475     /usr/lib/ld-2.17.so
46bda000-46bdb000 rw-p 0001f000 fd:01 401475     /usr/lib/ld-2.17.so
46bdd000-46d95000 r-xp 00000000 fd:01 401867     /usr/lib/libc-2.17.so
46d95000-46d96000 ---p 001b8000 fd:01 401867     /usr/lib/libc-2.17.so
46d96000-46d98000 r--p 001b8000 fd:01 401867     /usr/lib/libc-2.17.so
46d98000-46d99000 rw-p 001ba000 fd:01 401867     /usr/lib/libc-2.17.so
46d99000-46d9c000 rw-p 00000000 00:00 0 
46e2a000-46e45000 r-xp 00000000 fd:01 402104     /usr/lib/libgcc_s-4.8.1-20130603.so.1
46e45000-46e46000 r--p 0001a000 fd:01 402104     /usr/lib/libgcc_s-4.8.1-20130603.so.1
46e46000-46e47000 rw-p 0001b000 fd:01 402104     /usr/lib/libgcc_s-4.8.1-20130603.so.1
b776e000-b776f000 rw-p 00000000 00:00 0 
b777f000-b7782000 rw-p 00000000 00:00 0 
b7782000-b7783000 r-xp 00000000 00:00 0          [vdso]
bfdb3000-bfdd4000 rw-p 00000000 00:00 0          [stack]
Aborted (core dumped)

calloc() zero-initializes the buffer, while malloc() leaves the memory uninitialized. ~StackOverflow: Difference between malloc and calloc?

This means when malloc is asked for memory for new_node, that memory contains garbage values. It is unclear where new_node->next points, so you need to initialize new_node->next to NULL at the same time you store the word in the node.

As it is now, when you traverse the list, when crawler gets to the end of the list, unless you were lucky enough to get zeroed memory, it will try to follow the next pointer to some random place in memory.

Hope that helps.

You must log in to answer this question.

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