0

Hi I have an issue with freeing the family memory in my code: when I run my code the issue seems to be from the free memory for the parents section but I am not sure what's wrong

void free_family(person *p)
{
// TODO: Handle base case
if (p == NULL){
    return;
}

// TODO: Free parents
free(p->parents[0]);
free(p->parents[1]);

// TODO: Free child
free(p);

}

2
  • At a quick glance, this will only free one person and its parents. Which may be adequate for 2 generations, but not 3. Commented Jan 28, 2021 at 14:23
  • oh I get it now alright this helped a lot thank you so much
    – Arsham
    Commented Jan 29, 2021 at 15:04

1 Answer 1

1

Since memory is allocated as a tree, the code needs to traverse the tree to find all the memory.

It needs to either delete the memory as it moves to the leaves, or it needs to go all the way to the leaves and then delete as it returns up the tree.

As @DinoCoderSaurus said, this code will only handle the first two generations.

1
  • thank you for the information this was really helpful :D
    – Arsham
    Commented Jan 29, 2021 at 15:04

You must log in to answer this question.

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