blob: 42707d231f78bb3b4e4362c7238193bceff3fd90 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5
6struct list {
7 struct list *next;
8};
9
10int main(void)
11{
12 int z=999;
13 int *y=&z;
14 int *x=NULL;
15 struct list *save;
16 struct list *lp;
17 int i;
18
19
20 printf("pointer to x is %p\n", x);
21 printf("pointer to y is %p\n", y);
22 x=malloc(sizeof(int)*2000);
23 printf("pointer to x is %p\n", x);
24 y=malloc(sizeof(int)*100);
25 printf("pointer to y is %p\n", y);
26 free(x);
27 free(y);
28 printf("about to free(0)\n");
29 free(0);
30
31 x=malloc(13);
32 printf("x = %p\n", x);
33 memcpy(x, "Small string", 13);
34 printf("0x%p test string1: %s\n", x, (char *)x);
35 y = realloc(x, 36);
36 printf("0x%p test string1: %s\n", y, (char *)y);
37 memcpy(y, "********** Larger string **********", 36);
38 printf("0x%p test string2: %s\n", y, (char *)y);
39 free(y);
40
41
42 printf("Allocate 100 nodes 500 bytes each\n");
43 save = 0;
44 for (i=0; i<100; i++) {
45 lp = malloc(500);
46 if (lp == 0) {
47 printf("loop 1: malloc returned 0\n");
48 goto Failed;
49 }
50 lp->next = save;
51 save = lp;
52 }
53
54 printf("freeing 100 nodes\n");
55 while (save) {
56 lp = save;
57 save = save->next;
58 free(lp);
59 }
60
61 printf("try realloc 100 times \n");
62 lp = 0;
63 for (i=1; i<=100; i++) {
64 lp = realloc(lp, i*200);
65 if (lp == 0) {
66 printf("loop 3: realloc returned 0\n");
67 goto Failed;
68 }
69 }
70 {
71 void *unused_ret = realloc(lp, 0);
72 (void) unused_ret;
73 }
74
75 printf("Allocate another 100 nodes 600 bytes each\n");
76 save = 0;
77 for (i=0; i<100; i++) {
78 lp = malloc(600);
79 if (lp == 0) {
80 printf("loop 2: malloc returned 0\n");
81 goto Failed;
82 }
83 lp->next = save;
84 save = lp;
85 }
86
87 printf("freeing 100 nodes\n");
88 while (save) {
89 lp = save;
90 save = save->next;
91 free(lp);
92 }
93
94
95 printf("alloc test PASSED\n");
96 exit(0);
97
98Failed:
99 printf("!!!!!!!!!!!! alloc test FAILED. !!!!!!!!!!!!!!!\n");
100 exit(1);
101}