What is memory leak?

Asked 13-Nov-2017
Viewed 941 times

1 Answer


0

Memory Leak :
A Memory leak occurs when we create a memory in heap areas and forget to delete it. And it also happens when we create any objects multiple times and we can’t accessed by the running source code.
And the most powerful disadvantage of memory leak is, it reduces the performance of the computer system and also waste the space of memory.

Here is the C Language example where we can demonstrate the concept of memory leak.
Code for allocation of Memory Leak

#include <stdlib.h>;
void memoryLeakFunction()
{
   int *ptr = (int *) malloc(sizeof(int)); // allocate dynamic memory
   ...................
   ..................... // Write some code here
   ....................
   return; /* Return without freeing ptr* var/
}

Code for avoiding Memory Leak allocated on heap area.

#include <stdlib.h>;
void avoidMemoryLeak()
{
   int *ptr = (int *) malloc(sizeof(int)); // allocate dynamic memory
  .................
  ................... // Write some code here
  ...................
   free(ptr); // free() is used for deallocate the memory
   return;
}