// Free `p` and all ancestors of `p`.
void free_family(person *p)
    while (p != NULL)

    // TODO: Handle base case

    // TODO: Free parents

    // TODO: Free child


I compared my code with others, and found the problem is up here. I think my method is reasonable, but it will lead to segmentation fault. please tell me why.

1 Answer 1


What makes most sense to me is that you're passing NULL as the argument of a function then checking inside if p is NULL, that might have caused the seg fault. I'd suggest you try your code with checking if the parents pointer is NULL instead and if yes then ignore them and just free the child.

You must log in to answer this question.

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