>It will be great help if you throw some lights on memory leak.
Put simply, a memory leak is when you allocate memory and don't release it. For long running programs, eventually you will run out of memory. This is a contrived memory leak:
Code:
#include <stdlib.h>
int main ( void )
{
malloc ( 1000000 );
return 0;
}
A reference to the memory is not saved, therefore free cannot be called on it, so there is no way to release it without relying on the calling process, which may or may not release all memory allocated to your program. Here is a more subtle and realistic memory leak (names changed to protect the guilty):
Code:
...
p = malloc(size * sizeof(*p));
...
/* Recalculate size */
...
p = realloc(p, size * sizeof(*p));
If realloc fails (a reasonable assumption in the program that this code was used), it returns NULL. NULL is then immediately assigned to p, and any reference to the memory that p pointed to before is lost. There is no way to access the memory to release it, so you have a leak.