Loading content...
Loading content...
A Memory Leak occurs when a program allocates memory dynamically but fails to release it after it is no longer needed.
As a result, the allocated memory remains occupied and cannot be reused by the program, leading to unnecessary memory consumption.
In C, memory is allocated dynamically using functions like:
plaintext
malloc()
calloc()
realloc()The allocated memory must be released using:
plaintext
free()If free() is not called, the memory remains allocated even though the program no longer uses it.
plaintext
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
ptr = (int *)malloc(sizeof(int));
*ptr = 100;
printf("%d\n", *ptr);
// free(ptr); Missing
return 0;
}The memory allocated by malloc() is never released.
This creates a memory leak.
plaintext
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
ptr = (int *)malloc(sizeof(int));
*ptr = 100;
printf("%d\n", *ptr);
free(ptr);
ptr = NULL;
return 0;
}plaintext
100Memory is properly released using free().
plaintext
int *ptr = (int *)malloc(sizeof(int));
/* free(ptr); missing */The allocated memory remains occupied.
plaintext
int *ptr = (int *)malloc(sizeof(int));
ptr = (int *)malloc(sizeof(int));The address of the first memory block is lost forever.
This memory can no longer be freed.
plaintext
int *ptr = (int *)malloc(sizeof(int));
if(error)
{
return;
}If the function exits before calling free(), memory leaks occur.
plaintext
int *ptr = (int *)malloc(sizeof(int));
ptr = NULL;The original memory address is lost and cannot be freed.
Increased memory usage.
Reduced system performance.
Application becomes slow.
Program may crash.
Server applications may run out of memory over time.
plaintext
int *ptr = (int *)malloc(sizeof(int));
free(ptr);Release memory when it is no longer required.
plaintext
free(ptr);
ptr = NULL;This prevents dangling pointers.
plaintext
ptr = malloc(sizeof(int));
ptr = malloc(sizeof(int));plaintext
free(ptr);
ptr = malloc(sizeof(int));plaintext
int *ptr = malloc(sizeof(int));
if(error)
{
return;
}plaintext
int *ptr = malloc(sizeof(int));
if(error)
{
free(ptr);
return;
}A simple rule:
plaintext
One malloc() → One free()
One calloc() → One free()
One realloc() → One free()plaintext
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *arr;
arr = (int *)malloc(5 * sizeof(int));
if(arr == NULL)
{
printf("Memory Allocation Failed");
return 0;
}
for(int i = 0; i < 5; i++)
{
arr[i] = i + 1;
}
for(int i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
free(arr);
arr = NULL;
return 0;
}plaintext
1 2 3 4 5Some popular tools used to detect memory leaks:
Valgrind (Linux)
Address Sanitizer (ASan)
Visual Studio Memory Profiler
Dr. Memory
What is Memory Leak?
A memory leak occurs when dynamically allocated memory is not released after use. The memory remains occupied and cannot be reused, causing unnecessary memory consumption and potentially leading to performance issues or program crashes.
How can we avoid it?
Always release dynamically allocated memory using free().
Set pointers to NULL after freeing.
Avoid overwriting pointers before freeing old memory.
Ensure every malloc(), calloc(), or realloc() has a corresponding free().
Use memory debugging tools like Valgrind to detect leaks.
Memory Leak occurs when allocated memory is not released.
It commonly happens with malloc(), calloc(), and realloc().
Memory leaks waste system resources and may crash long-running programs.
Always use free() after memory usage.
Setting pointers to NULL after free() is a good programming practice.