0

I tried two methods but still, have a memory leak and also I didn't know how I know every step succeed or not. it supposes return true if free all memory and return false is not where I must write return false

    bool unload(void)
{
    for (int n = 0; n < 101; n++)
    {
        destroy(table[n]);  
    }
    return true;
}
void destroy(node *sample)
{
    node *temp = sample;
    if (temp->next == NULL)
    {
        free(temp);
    }
    else
    {
        destroy(temp->next);
    }
}

when I write this code for free malloc memory I get this enter image description here

bool unload(void)
{
    node *temp = NULL;
    for (int n = 0; n < 101; n++)
    {
        temp = table[n];
        for (node *current = table[n]; current->next != NULL; current = temp)
        {
            temp = temp->next;
            free(current);
        }
    }
    return true;
}

when I tried this I get enter image description here

1

First version makes little sense to me.

In second version, you stop when current->next becomes NULL. But first, if current is NULL (small dict would have that), you get a segmentation fault. Second, what about current?

Keep freeing nodes as long as current != NULL.

You must log in to answer this question.

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