Memory Leak Tutorials
Programming
Memory Leak in C
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.
Why Does Memory Leak Occur?
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.
Example of 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); Missing
return 0;
}Problem
The memory allocated by malloc() is never released.
This creates a memory leak.
Correct Program
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;
}Output
plaintext
100Memory is properly released using free().
Common Causes of Memory Leaks
1. Forgetting to Free Memory
plaintext
int *ptr = (int *)malloc(sizeof(int));
/* free(ptr); missing */The allocated memory remains occupied.
2. Overwriting a Pointer
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.
3. Returning Without Freeing Memory
plaintext
int *ptr = (int *)malloc(sizeof(int));
if(error)
{
return;
}If the function exits before calling free(), memory leaks occur.
4. Lost References
plaintext
int *ptr = (int *)malloc(sizeof(int));
ptr = NULL;The original memory address is lost and cannot be freed.
Effects of Memory Leaks
Increased memory usage.
Reduced system performance.
Application becomes slow.
Program may crash.
Server applications may run out of memory over time.
How to Avoid Memory Leaks?
1. Always Free Allocated Memory
plaintext
int *ptr = (int *)malloc(sizeof(int));
free(ptr);Release memory when it is no longer required.
2. Set Pointer to NULL After Freeing
plaintext
free(ptr);
ptr = NULL;This prevents dangling pointers.
3. Do Not Overwrite Pointers
Wrong
plaintext
ptr = malloc(sizeof(int));
ptr = malloc(sizeof(int));Correct
plaintext
free(ptr);
ptr = malloc(sizeof(int));4. Free Memory Before Returning
Wrong
plaintext
int *ptr = malloc(sizeof(int));
if(error)
{
return;
}Correct
plaintext
int *ptr = malloc(sizeof(int));
if(error)
{
free(ptr);
return;
}5. Check Every malloc() With a Matching free()
A simple rule:
plaintext
One malloc() → One free()
One calloc() → One free()
One realloc() → One free()Example Without Memory Leak
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;
}Output
plaintext
1 2 3 4 5Detecting Memory Leaks
Some popular tools used to detect memory leaks:
Valgrind (Linux)
Address Sanitizer (ASan)
Visual Studio Memory Profiler
Dr. Memory
Interview Answer
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
NULLafter freeing.Avoid overwriting pointers before freeing old memory.
Ensure every
malloc(),calloc(), orrealloc()has a correspondingfree().Use memory debugging tools like Valgrind to detect leaks.
Summary
Memory Leak occurs when allocated memory is not released.
It commonly happens with
malloc(),calloc(), andrealloc().Memory leaks waste system resources and may crash long-running programs.
Always use
free()after memory usage.Setting pointers to
NULLafterfree()is a good programming practice.