I've combed through my code for this pset and reviewed the walkthroughs and shorts countless times and am now resigned to admit I need help.

Thanks to Peter, I'm no longer leaking any memory (yay!), but I'm still having trouble with the check function, and valgrind confirms that that is indeed where the problem lies. I get this error notice: "Uninitialised value was created by a stack allocation" at line 89. I understand what it's telling me but can't figure out how to fix it.

Below I've copied just that section of my code. Thanks in advance for your help.

Link to my old code (for reference): https://gist.github.com/choaimeloo/fa37314cc4f278f284a10fbc48cca3b1

// Returns true if word is in dictionary else false
bool check(const char *word)
    // Creates copy of word on which hash function can be performed
    char word_copy[LENGTH + 1];
    for (int i = 0, n = strlen(word); i < n; i++)
        word_copy[i] = tolower(word[i]);
    // Initializes index for hashed word
    int h = hash_index(word_copy);
    // Sets head to point to hashtable by index/bucket
    node *head = hashtable[h];
    // Sets cursor to point to same location as head
    node *cursor = head;

    // If the word exists, you should be able to find in dictionary data structure.
    // Check for word by asking, which bucket would word be in? hashtable[hash(word)]
    // While cursor does not point to NULL, search dictionary for word.
    while (cursor != NULL)
        // If strcasecmp returns true, then word has been found
        if (strcasecmp(cursor->word, word_copy) == 0)
            return true;
        // Else word has not yet been found, advance cursor
            cursor = cursor->next;
    // Cursor has reached end of list and word has not been found in dictionary so it must be misspelled
    return false;

You are leaking memory in this part of your code:

// Inserts word into list (at the beginning & without losing any links)
node *head = malloc(sizeof(node));
head = hashtable[h];
new_node->next = head;
  • In the first line you allocate memory for a new node, and you save it's position in a new node pointer called head.
  • But in the second line you change the value of head, losing the only reference to the memory you just allocated.

If the hash value has not occurred earlier, you put the address of your new node in your local variable head.

// If bucket is empty, insert the first node
if (head == NULL)
    head = new_node;

The place where you should have stored the address of your new node is a place you used a couple of lines higher ... (So I know that you can figure that out by yourself.)

In the else part you make the same mistake (after correctly storing the position of the old head node in your new node)), so you should be able to fix that by yourself.

Finally, to quote Cliff B:

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |

You must log in to answer this question.

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