Apply for Zend Framework Certification Training

c




< Passing pointers to functions and returning pointers from functions Last >



Memory Leaks: Understanding and Avoiding
 
What is a Memory Leak?
A memory leak occurs when a program allocates memory dynamically but fails to release (free) it after use.
That memory remains occupied and cannot be reused, leading to:
Reduced available memory
Slower performance
Program crashes (in extreme cases)
 
Where does it happen?
Memory leaks commonly occur in Heap memory when using:
malloc(),calloc(),realloc()
 
Simple Example of Memory Leak
#include <stdio.h>
#include <stdlib.h>
int main() {
    int *ptr;
    ptr = (int*) malloc(5 * sizeof(int)); // memory allocated
    // Forgot to free memory
    return 0;
}
Problem:
Memory is allocated but never released using free()
Correct Version (No Memory Leak) 
#include <stdio.h>
#include <stdlib.h>
int main() {
    int *ptr;
    ptr = (int*) malloc(5 * sizeof(int));
    // use memory
    for(int i = 0; i < 5; i++) {
        ptr[i] = i;
    }
    free(ptr);  // memory released
    return 0;
}
 
Common Causes of Memory Leaks
1. Forgetting to free memory
int *p = malloc(sizeof(int));
// no free(p);
 
2. Losing reference to allocated memory
int *p = malloc(sizeof(int));
p = NULL;  // original memory lost → leak
 
3. Reassigning pointer without freeing
int *p = malloc(sizeof(int));
p = malloc(sizeof(int));  // previous memory lost
 
4. Improper use of realloc()
int *p = malloc(10 * sizeof(int));
p = realloc(p, 20 * sizeof(int)); // if fails → memory leak risk
 
Safer way:
int *temp = realloc(p, 20 * sizeof(int));
if(temp != NULL) {
    p = temp;
}
5. Memory allocated in loops
for(int i = 0; i < 100; i++) {
    int *p = malloc(sizeof(int)); // leak every iteration
}
 
Effects of Memory Leaks
? Increased RAM usage
? System slowdown
? Application crash
? Poor performance in long-running programs
 
How to Avoid Memory Leaks
? Always free allocated memory
free(ptr);
? Set pointer to NULL after freeing
free(ptr);
ptr = NULL;
? Use proper memory tracking
Keep track of all allocations
Free memory before exiting functions
? Use tools to detect leaks
Valgrind (Linux)
AddressSanitizer (GCC/Clang)
? Follow ownership rules
The function that allocates memory should either:
free it, OR
clearly transfer responsibility
 
Real-Life Analogy
 
Think of memory like renting a room:
malloc() → Renting a room
Using memory → Staying in the room
free() → Vacating the room
 
If you don’t vacate, you keep paying rent → memory leak
"Every malloc() must have a corresponding free()"
 
Short Summary
Concept Description
Memory Leak Allocated memory not freed
Cause Missing free() or lost pointer
Solution Use free() properly
Tools Valgrind, Sanitizers

< Passing pointers to functions and returning pointers from functions Last >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top