summaryrefslogtreecommitdiff
path: root/test/malloc/testmalloc.c
blob: caf08a9e17426494d3ee81a09bc9360d43e7972b (plain)
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
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


struct list {
	struct list *next;
};

int main(void)
{
	int z=999;
	int *y=&z;
	int *x=NULL;
	struct list *save;
	struct list *lp;
	int i;


	printf("pointer to x is %p\n", x);
	printf("pointer to y is %p\n", y);
	x=malloc(sizeof(int)*2000);
	printf("pointer to x is %p\n", x);
	y=malloc(sizeof(int)*100);
	printf("pointer to y is %p\n", y);
	free(x);
	free(y);
	printf("about to free(0)\n");
	free(0);
	
	x=malloc(13);
	printf("x = %p\n", x);
	memcpy(x, "Small string", 13);
	printf("0x%p test string1: %s\n", x, (char *)x);
	y = realloc(x, 36);
	printf("0x%p test string1: %s\n", y, (char *)y);
	memcpy(y, "********** Larger string **********", 36);
	printf("0x%p test string2: %s\n", y, (char *)y);
	free(y);
	
	
	printf("Allocate 100 nodes 500 bytes each\n");
	save = 0;
	for (i=0; i<100; i++) {
		lp = malloc(500);
		if (lp == 0) {
			printf("loop 1: malloc returned 0\n");
			goto Failed;
		}
		lp->next = save;
		save = lp;
	}
	
	printf("freeing 100 nodes\n");
	while (save) {
		lp = save;
		save = save->next;
		free(lp);
	}
	
	printf("try realloc 100 times \n");
	lp = 0;
	for (i=1; i<=100; i++) {
		lp = realloc(lp, i*200);
		if (lp == 0) {
			printf("loop 3: realloc returned 0\n");
			goto Failed;
		}
	}
	{
	void *unused_ret = realloc(lp, 0);
	(void) unused_ret;
	}
	
	printf("Allocate another 100 nodes 600 bytes each\n");
	save = 0;
	for (i=0; i<100; i++) {
		lp = malloc(600);
		if (lp == 0) {
			printf("loop 2: malloc returned 0\n");
			goto Failed;
		}
		lp->next = save;
		save = lp;
	}
	
	printf("freeing 100 nodes\n");
	while (save) {
		lp = save;
		save = save->next;
		free(lp);
	}
	

	printf("alloc test PASSED\n");
	exit(0);

Failed:
	printf("!!!!!!!!!!!! alloc test FAILED. !!!!!!!!!!!!!!!\n");
	exit(1);
}