-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
49 lines (43 loc) · 1.39 KB
/
Copy pathtest.c
File metadata and controls
49 lines (43 loc) · 1.39 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
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "malloc.h"
#define NUM_ALLOCS 10000
#define MAX_SIZE 10240
#define MAX_ITERATIONS 1000000
void random_alloc_free_test() {
srand((unsigned int)time(NULL));
void* pointers[NUM_ALLOCS] = {NULL};
for (int i = 0; i < MAX_ITERATIONS; ++i) {
int index = rand() % NUM_ALLOCS;
if (pointers[index] == NULL) {
// Allocate memory
size_t size = (size_t)(rand() % MAX_SIZE) + 1;
pointers[index] = mymalloc(size);
if (pointers[index] != NULL) {
printf("Allocated memory of size %zu at address %p\n", size, pointers[index]);
} else {
fprintf(stderr, "Allocation failed for size %zu\n", size);
}
} else {
// Free memory
printf("Freeing memory at address %p\n", pointers[index]);
myfree(pointers[index]);
pointers[index] = NULL;
}
}
// Free remaining allocated memory
for (int i = 0; i < NUM_ALLOCS; ++i) {
if (pointers[i] != NULL) {
printf("Freeing remaining memory at address %p\n", pointers[i]);
myfree(pointers[i]);
pointers[i] = NULL;
}
}
}
int main() {
printf("Starting random allocation and deallocation test...\n");
random_alloc_free_test();
printf("Test complete.\n");
return 0;
}