0

I'm getting a "double free or corruption" error when running this code. I don't know in which function the error is. I get the error after the code has run through check, before it gets to the unload function. If anyone could help that would be great.

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// Represents a hash table
node *hashtable[N];

//Keeps track of the number of words inserted into the dictionary
int dic_count = 0;



// Hashes word to a number between 0 and 25, inclusive, based on its first letter
unsigned int hash(const char *word)
{
    return tolower(word[0]) - 'a';
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // Initialize hash table
    for (int i = 0; i < N; i++)
    {
        hashtable[i] = NULL;
    }

    // Open dictionary
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        unload();
        return false;
    }

    // Buffer for a word
    char word[LENGTH + 1] = {'\0'};

    // TODO

    int innex = 0;

    // Create a new node called header and set its value to NULL
    node *head = malloc(sizeof(node));

    // Insert words into hash table
    while (fscanf(file, "%s", word) != EOF)
    {
        node *new_node = malloc(sizeof(node));
        if(new_node == NULL)
        {
            unload();
            return false;
        }
        else
        {
            strcpy(new_node->word, word);
            innex = hash(new_node->word);
            if(hashtable[innex] == NULL)
            {
                hashtable[innex] = new_node;
            }
            else
            {
                head = hashtable[innex];
                hashtable[innex] = new_node;
                new_node->next = head;
            }

            dic_count++;
        }


        free(new_node);

    }

    free(head);

    // Close dictionary
    fclose(file);

    // Indicate success
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    // TODO
    return dic_count;
}

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    // TODO


    // Iterate through the hashtable and compare each word

    int innex = hash(word);

    node *cursor = hashtable[innex];
    while(cursor != NULL)
    {
        if(strcasecmp(word, cursor->word))
        {
            printf("Word Found\n");
            return true;
        }

        cursor = cursor->next;
    }

    printf("Word was not found\n");


    return false;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    // TODO

    node *cursor = NULL;

    for(int i = 0; i < N; i++)
    {
        cursor = hashtable[i];

        while(cursor != NULL)
        {
            node *tmp = cursor;
            cursor = cursor->next;
            free(tmp);
        }
    }


    return false;
}
1

If hashtable[innex] == NULL, your new_node->next is never initialised, it stays at the value that happened to be there when you called malloc. Might show up as "condition depends on uninitialised value", or as a segmentation fault. There's nothing special about the first node in this algorithm, and you can use the non-NULL code there as well.

Don't allocate for your head pointer, as you never use that allocated node. You don't even need head, if you assign to new_node->next first, and only then make new_node the new list head. But anyway, you do not use that malloced node from the beginning, so don't allocate it.

Do not free in load (that's what unload is for). You likely free nodes that you just installed in the hash table. This would result in use-after-free, or double-free, might be the cause of the error message you are experiencing. As you allocate all nodes in the same memory location (since you free them immediately), you might also create an infinite loop, having the only node (marked as unused) point to itself as next node.

You must log in to answer this question.

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