Browsed by
Tag: memory leak

How memory leak occurs?

How memory leak occurs?

Memory leak is a problem that happens in programs when there is some unnecessary memory consumption or when program forgets to release the memory it used.
For example

void karthi(void)
{
int *ngk = malloc(20);
return;
}

int main(void)
{
while(1)
karthi();
return;
}

In this program from the main function we are calling the function called karthi(). Inside that function we are allocating 10 bytes for a integer pointer using malloc function. But before using that allocated memory space that function ends and the allocated space doesn’t used and it still remains in memory as unused. This is called as memory leak.