0

My check function compiles, but gets stuck in a my while-loop when running. Using debugger, it looks like the values are set correctly for "word" in the node ("LA" from lalaland.txt), but the loop never breaks (Head is never NULL).

My thoughts / questions on whats wrong: 1) Am I not iterating through the linked list correctly? head = head->next; should be doing this, but do I also need to change "word" in the "head" node? 2) Would "head" as a node be null, or should I look for when head->next is null bc that would indicate end of list?

Thank you for direction in advance!

// Returns true if word is in dictionary, else false

//Get hashtable value of word.
hash_value = hash(word);
//Create new node, head to use as cursor to traverse linkedlist
node *head = hashtable[hash_value];

//Loop to look for matching words
while(head->next!= NULL)
{

   if(strcasecmp(head->word, word)==0)
    {
        return true;
    }
    else
    {
    head = head->next;
    }
}
return false;

Struct code below

typedef struct node
{
    char word[LENGTH + 1];
    struct node* next;
}
node;
int hash_value = 1;
node* hashtable[15625];

Valgrind output for lalaland.txt

dictionary.c:57:17: runtime error: member access within null pointer of type 'node' (aka 'struct node')
==19852== 
==19852== Process terminating with default action of signal 11 (SIGSEGV)
==19852==  Access not within mapped region at address 0x30
==19852==    at 0x4226DD: check (dictionary.c:57)
==19852==    by 0x4213F3: main (speller.c:112)
==19852==  If you believe this happened as a result of a stack
==19852==  overflow in your program's main thread (unlikely but
==19852==  possible), you can try to increase the size of the
==19852==  main thread stack using the --main-stacksize= flag.
==19852==  The main thread stack size used in this run was 8388608.
==19852== 
==19852== HEAP SUMMARY:
==19852==     in use at exit: 1,136 bytes in 2 blocks
==19852==   total heap usage: 143,093 allocs, 143,091 frees, 8,014,232 bytes allocated
==19852== 
==19852== LEAK SUMMARY:
==19852==    definitely lost: 0 bytes in 0 blocks
==19852==    indirectly lost: 0 bytes in 0 blocks
==19852==      possibly lost: 0 bytes in 0 blocks
==19852==    still reachable: 1,136 bytes in 2 blocks
==19852==         suppressed: 0 bytes in 0 blocks
==19852== Reachable blocks (those to which a pointer was found) are not shown.
==19852== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==19852== 
==19852== For counts of detected and suppressed errors, rerun with: -v
==19852== ERROR SUMMARY: 1564399 errors from 3 contexts (suppressed: 0 from 0)

1 Answer 1

1

check should not allocate any node (just set your traversal pointer to hashtable[hash_value])

Your problem is likely in the load function, where you allocate your nodes and place them in the hash table. It seems you are creating some loop in the linked list. Maybe you place the next pointer incorrectly, or you re-use a node instead of creating another.

3
  • Thanks Blauelf, I think that helped me solve my initial issues in check. In running the lalaland.txt now, it looks like I'm mis-interpreting a few words and printing them as misspelled (example output text: after, A, a, TECHNO, a, which). There is also a "member access" error at the while loop.
    – Jake
    Commented Jul 12, 2018 at 21:12
  • while(head->next!= NULL) needs to be while(head!= NULL)
    – Blauelf
    Commented Jul 13, 2018 at 12:58
  • That, along with removing "free(new_node);" from within my main loop in Load, helped fix the issues. It looks like I'm identifying incorrect words correctly, need to do Unload now to see final output. Thanks!
    – Jake
    Commented Jul 13, 2018 at 14:52

You must log in to answer this question.

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