Allocating heap memory dynamically with malloc/calloc without releasing it with free.
Dynamically allocated heap memory remains allocated even after all pointers referencing it have gone out of scope.
Forgetting to call `free()` before function returns or re-assigning a heap pointer without freeing its original allocation.
1#include <stdlib.h>2 3void process_data() {4 char *buffer = malloc(1024); // Heap allocation5 // Forgetting free(buffer) before function exits!6}7 8int main() {9 for (int i = 0; i < 10000; i++) {10 process_data(); // Leaks 1024 bytes per iteration11 }12 return 0;13}1#include <stdlib.h>2#include <stdio.h>3 4void process_data() {5 char *buffer = malloc(1024);6 if (buffer == NULL) return;7 8 // Perform work with buffer...9 10 free(buffer); // Properly release heap memory11 buffer = NULL;12}13 14int main() {15 for (int i = 0; i < 10000; i++) {16 process_data();17 }18 printf("Memory cleanly freed.\n");19 return 0;20}Simulate standard system builds to trigger compiler trace records and track memory crashes locally.
Every call to `process_data()` allocates 1024 bytes on the heap. When `process_data()` finishes, `buffer` variable on stack disappears, leaving 1024 bytes orphaned on heap.