What is memory leak?

2 votes
173 views
Tarun Gautam asked 13-Nov-2017 in programming by Tarun Gautam

What is memory leak?

1 Answer

0 votes
Arti Mishra answered 12-Aug-2018 by Arti Mishra
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;
}