What is Memory Leak ? How we can avoid ?
Memory Leak:
http://en.wikipedia.org/wiki/Memory_leak
Memory leak occurs when programmers create a memory in heap and forget to delete it.
Memory leaks are particularly serious issues for programs like daemons and servers which by definition never terminate.
To avoid memory leaks, memory allocated on heap should always be freed when no longer needed.
Memory Leak:
http://en.wikipedia.org/wiki/Memory_leak
Memory leak occurs when programmers create a memory in heap and forget to delete it.
Memory leaks are particularly serious issues for programs like daemons and servers which by definition never terminate.
#include <stdlib.h> void f() { int *ptr = ( int *) malloc ( sizeof ( int )); /* Do some work */ return ; /* Return without freeing ptr*/ } |
/* Function without memory leak */
#include <stdlib.h>;
void
f()
{
int
*ptr = (
int
*)
malloc
(
sizeof
(
int
));
/* Do some work */
free
(ptr);
return
;
}
Memory Leak:
char* str = (char*)malloc(sizeof(char) * 4);
str="abc";
To avoid Memory Leak:
char* str = (char*)malloc(sizeof(char) * 4);
strcpy(str, "abc");
str[3] = '\0';
Memory Leak Situaltion:
ReplyDeleteOrphaned Allocation:--
It is characterized by allocation of a memory segment whose address is not preserved for later deallocation.
First,
char* p;
...
p = malloc(strlen(s)+1);
p=s;
The memory for a copy of the string s is allocated, yet the pointer p is merely set to point to the same place as the pointer s. Even if free(p) is subsequently invoked, it does not free the segment previously allocated. The program may seem to work (at least p seems to be the "correct" string), but trying to free p and then s may induce a crash because some operating systems (like LINUX) will terminate a program that is trying to free a segment not currently allocated.
A similar situation holds for
char* p;
...
p = malloc(4);
p = "abc";
Hidden Allocation:--
It is characterized by a call to another function to deliver an object without realizing that the object has been created on the heap. As an example, consider the standard function strdup(). It duplicates a string and returns a pointer to it. The duplicated string is created on the heap. If not deallocated later, the memory leaks.
Undetermined Ownership:--
The owner of a memory segment is (as far as we are concerned) the module that is responsible for deallocating it. The undetermined ownership problem is characterized by a module obtaining a memory segment without realizing that it is now the owner and therefore responsible for its deallocation. A typical scenario runs along the following lines. A module A requests a module B to provide an object. The object is created dynamically and thus it cannot be owned by B. But the programmer of module A is not aware of the ownership issue - thinking perhaps that the object will be deallocated by some other module somewhere else, for the programmer is just "using" the object here. This confusion is usually compounded by the path of an object being more complex; for example, an object might have been created by a module D, which passed it to C, which passed it to B, which passed to A. Hence the programmer responsible for the design and/or coding of A may not even know the object's origin, let alone its intended future or who should deallocate it. The further apart the place where a dynamic object is created and the place where it is used, the more likely the object will be forgotten and undeallocated.