-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemhog.c
More file actions
36 lines (33 loc) · 878 Bytes
/
memhog.c
File metadata and controls
36 lines (33 loc) · 878 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
if(argc != 2){
printf("Please pass MBs of memory to be allocated. Exiting...\n");
exit(EXIT_FAILURE);
}
int i, x = atoi(argv[1]);
char *p;
printf("Trying to allocate %dMB with this program\n", x);
int y = x/5;
x = x-(5*y);
for(i = 0; i < y; ++i){
p = malloc(5 * (1<<20));
if(p == NULL){
printf("Malloc failed at %d MB\n", (i+1)*5);
return 0;
}
memset(p, 0, 5*(1<<20));
printf("%dMB allocated\n", (i+1)*5);
}
p = malloc(x * (1<<20));
if(p == NULL){
printf("Malloc failed at %d MB\n", (i)*5+x);
return 0;
}
memset(p, 0, x*(1<<20));
printf("%dMB allocated\n", (i)*5+x);
printf("Done!\n");
return 0;
}