-
Notifications
You must be signed in to change notification settings - Fork 8
/
_malloc.c
90 lines (70 loc) · 1.25 KB
/
_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
OK
OK
OK
OK
*/
#include <stdio.h>
#include <stdlib.h>
#define N 10000
#define check(c) \
do { \
if (c) \
printf("OK\n"); \
else \
printf("NG\n"); \
} while (0)
int
my_rand(int mod)
{
static int seed = 876543;
return seed = (seed * 11 + 5) % mod;
}
int
test(int *list[N])
{
int i, j;
for (i = 0; i < N; ++i) {
int s;
if (list[i] == NULL) {
continue;
}
s = list[i][0];
for (j = 0; j < s; ++j) {
if (s != list[i][j]) {
return 0;
}
}
}
return 1;
}
int main()
{
int i, j;
int *list[N];
// initialize
for (i = 0; i < N; ++i) {
int s = my_rand(100);
list[i] = malloc((s + 1) * sizeof(int));
for (j = 0; j < s; ++j) {
list[i][j] = s;
}
}
check(test(list));
for (i = 0; i < N; i+=3) {
free(list[i]);
list[i] = NULL;
}
check(test(list));
for (i = 1; i < N; i+=3) {
free(list[i]);
list[i] = NULL;
}
check(test(list));
for (i = 2; i < N; i+=3) {
free(list[i]);
list[i] = NULL;
}
check(test(list));
return 0;
}