-
Notifications
You must be signed in to change notification settings - Fork 0
/
malloc.c
52 lines (47 loc) · 815 Bytes
/
malloc.c
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void print_size(size_t size)
{
if(size / 1024 == 0) {
printf("malloc %d Bytes success\n", size);
return;
}
size /= 1024;
if(size / 1024 == 0) {
printf("malloc %d K success\n", size);
return;
}
size /= 1024;
if(size / 1024 == 0) {
printf("malloc %d M success\n", size);
return;
}
size /= 1024;
if(size / 1024 == 0) {
printf("malloc %d G success\n", size);
return;
}
size /= 1024;
printf("malloc %d T success\n", size);
}
int malloc_test()
{
size_t step = 1024;
for(size_t i = 0; ; i+=step) {
void *p = malloc(i);
if(p == NULL) {
perror("malloc failed.");
exit(-1);
} else {
print_size(i);
free(p);
}
if(i / step > 1024)
step *= 1024;
}
}
int main(int argc, char *argv[])
{
malloc_test();
}