-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_leak.c
More file actions
38 lines (31 loc) · 1.06 KB
/
memory_leak.c
File metadata and controls
38 lines (31 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// Function that leaks a small amount of memory
void leak_small_memory() {
// Allocate 1 KB of memory and never free it
char *leaked_ptr = (char *)malloc(1024);
if (leaked_ptr != NULL) {
// Use the memory to avoid compiler optimizations
memset(leaked_ptr, 0xAB, 1024);
strcpy(leaked_ptr, "This memory will never be freed");
}
// Intentionally not freeing the memory - this is the leak
}
int main() {
int iteration = 0;
printf("Starting memory leak program...\n");
printf("This program will leak 1 KB of memory every 1 minute\n");
printf("Use tools like 'valgrind' or 'ps' to monitor memory growth\n");
printf("Press Ctrl+C to exit\n\n");
while (1) {
leak_small_memory();
iteration++;
printf("Iteration %d: Leaked 1 KB (Total leaked: %d KB)\n",
iteration, iteration);
// Sleep for 1 minute (60 seconds) between leaks
sleep(60);
}
return 0;
}