0 votes
in C Plus Plus by
What is memory leak? Why it should be avoided

1 Answer

0 votes
by

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.

/* Function with memory leak */
#include <stdlib.h>
  
void f()
{
    int* ptr = (int*)malloc(sizeof(int));
  
    /* Do some work */
  
    return; /* Return without freeing ptr*/
}

Related questions

0 votes
asked Jan 6 in C Plus Plus by GeorgeBell
0 votes
asked Jan 4 in C Plus Plus by GeorgeBell
...