memory leak in c/c++

Is it necessary to mach the count of malloc() and free() in your C program to avoid memory leaks.?
If number of malloc() and free() are not equal then would it result in memory leak always ??
You need to also account for calloc() and realloc() (and strdup()).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdlib.h>

void foo()
{
    void* p1 = realloc( NULL, 100 ) ;
    void* p2 = calloc( 10, 10 ) ;
    // leak 
}

void bar()
{
    void* p = malloc(100) ;
    if(p) realloc( p, 0 ) ;
    // no leak 
}
Topic archived. No new replies allowed.