0
void free_family(person *p)
{
if(p->parents[0] == NULL && p->parents[1] == NULL)
{
free(p);
p = NULL;
}
else
{
free_family(p->parents[0]);
free_family(p->parents[1]);
}

}

With this, Valgrind says:

=13099== HEAP SUMMARY:

==13099== in use at exit: 72 bytes in 3 blocks

==13099== total heap usage: 7 allocs, 4 frees, 168 bytes allocated

==13099==

==13099== 24 bytes in 1 blocks are indirectly lost in loss record 1 of 3

==13099== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)

==13099== by 0x1091F4: create_family (inheritance.c:43)

==13099== by 0x10920D: create_family (inheritance.c:49)

==13099== by 0x1091B8: main (inheritance.c:30)

==13099==

==13099== 24 bytes in 1 blocks are indirectly lost in loss record 2 of 3

==13099== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)

==13099== by 0x1091F4: create_family (inheritance.c:43)

==13099== by 0x10921C: create_family (inheritance.c:50)

==13099== by 0x1091B8: main (inheritance.c:30)

==13099==

==13099== 72 (24 direct, 48 indirect) bytes in 1 blocks are definitely lost in loss record 3 of 3

==13099== at 0x4848899: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)

==13099== by 0x1091F4: create_family (inheritance.c:43)

==13099== by 0x1091B8: main (inheritance.c:30)

==13099==

==13099== LEAK SUMMARY:

==13099== definitely lost: 24 bytes in 1 blocks

==13099== indirectly lost: 48 bytes in 2 blocks

==13099== possibly lost: 0 bytes in 0 blocks

==13099== still reachable: 0 bytes in 0 blocks

==13099== suppressed: 0 bytes in 0 blocks

==13099==

==13099== For lists of detected and suppressed errors, rerun with: -s

==13099== ERROR SUMMARY: 3 errors from 2 contexts (suppressed: 0 from 0)

Adding these lines at the end however fixes the problem but I don't still quite understand what exactly is going on

if(p != NULL)
{
free(p);
}

Could someone please explain what was wrong here?

Thanks in advance for your help!!

1 Answer 1

0

I numbered my comments, please read those in order first then read the explanation at the bottom.

void free_family(person *p)
{
    // 1. The first time this is called p's parent's are not NULL. It does have parents.
    // 3. the second generation also has parents, execute the else block again (2.)
    // 4. the third generation's parents do equal NULL, they don't have parents, so free(p)
    if(p->parents[0] == NULL && p->parents[1] == NULL)
    {
        free(p);
        p = NULL; // side note: The teacher mentioned after you free something you shouldn't touch it
    }
    else
    {
        // 2. So we go here and continue calling free_family on the next generation of parents
        free_family(p->parents[0]);
        free_family(p->parents[1]);
    }
}

// What your code does:
// o  o o  o <-- third gen p's parents == NULL. free(p) on each of these (they free themselves only and all the code has been executed)
//  \/   \/
//   o   o <-- second gen p's also have parents, continue calling free_family on their parents
//    \ /
//     o <-- the first "p" has parents. Go into the else block and call free_family on it's parents
//
// Result: 7 allocs, 4 frees, like your error says

You must log in to answer this question.

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