Skip to content

Commit

Permalink
test memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
Stool233 committed Jan 9, 2022
1 parent b66d298 commit 181530b
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions malloc_test2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>


#define K (1024 * 500000)
#define MAXNUM 1

int main() {

printf("Welcome to memory leak example::%d\n", getpid());

char *ptrs[MAXNUM];
int i;
// malloc large block memory
for (int i = 0; i < MAXNUM; i++) {
ptrs[i] = (char *)malloc(1 * K);
memset(ptrs[i], 0, 1 * K);
}
// never free only 1B memory leak, what it will impact to the system?
char *tmp1 = (char *)malloc(1);
memset(tmp1, 0, 1);

printf("%s\n", "malloc done");
getchar();

printf("%s\n", "start free memory");

// for (i = 0; i < MAXNUM; i++) {
// free(ptrs[i]);
// }
// reverse work?
for (i = MAXNUM-1; i >= 0; i--) {
free(ptrs[i]);
}
printf("%s\n", "free done");

getchar();

return 0;
}

0 comments on commit 181530b

Please sign in to comment.