0

I have executed following code for free family function -

 void free_family(person *p)
{
    // TODO: Handle base case
    if(p->parents[0] == NULL && p->parents[1] == NULL)
    {
        free(p);
        return;
    }

    // TODO: Free parent
    else
    {
        for (int i = 0; i < 2; i++)
        {
            free_family(p->parents[i]);
        }
    }

    // TODO: Free child
}

I have seen the provided answer but I cannot figure out why this code does not work.

valgrind says - total heap usage: 7 allocs, 4 frees, 168 bytes allocated

Please help me .

0

When I am executing the code I am freeing the parents but not the child so here is the code that works.

void free_family(person *p)
{
    // Recurse through the parents until pointer to parents is not null
    if(p->parents)
    {
        for (int i = 0; i < 2; i++)
        {
            free_family(p->parents[i]);
        }

    }
    // frees the allocated byte for person
    free(p);
    return;


}

if anyone need further explanation I can tell . Ask me.

You must log in to answer this question.

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