链表管理内存
1 //mem.h 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 6 void *mymalloc(size_t size); 7 8 void myfree(void *p); 9 10 void *myrealloc(void *p, size_t size); 11 12 //记录分配的内存地址的大小,地址 13 struct MEM 14 { 15 void *p; 16 int size; 17 }; 18 19 typedef struct LinkNode 20 { 21 struct MEM *pmem;//保存指针 22 struct LinkNode *pNext;//指向下一个节点 23 }node,*PNODE; 24 25 struct LinkNode *phead; 26 27 28 PNODE addback(PNODE phead, struct MEM *pmem); 29 PNODE change(PNODE phead, void *pfind, struct MEM *pnewmem); 30 PNODE findit(PNODE phead, void *pfind); 31 PNODE deletenode(PNODE phead, void *paddr); 32 PNODE deleteall(PNODE phead); 33 void showall(PNODE phead); 34 35 void getinfo(PNODE phead); 36 37 38 39 //mem.c 40 #include "mem.h" 41 struct LinkNode *phead = NULL; 42 43 44 void *mymalloc(size_t size) 45 { 46 47 void *p = malloc(size); 48 //printf("分配内存地址%p,大小%d\n", p, size); 49 struct MEM *pmem = malloc(sizeof(struct MEM)); 50 pmem->p = p; 51 pmem->size = size; 52 phead = addback(phead, pmem); 53 return p; 54 } 55 56 void myfree(void *p) 57 { 58 PNODE *px = findit(phead, p); 59 if (px == NULL) 60 { 61 return; 62 } 63 else 64 { 65 phead = deletenode(phead, p); 66 free(p); 67 } 68 } 69 70 void *myrealloc(void *p, size_t size) 71 { 72 73 void *pt = realloc(p, size); 74 struct MEM mymem; 75 mymem.p = pt; 76 mymem.size = size; 77 change(phead, p, &mymem); 78 return pt; 79 } 80 81 82 PNODE addback(PNODE phead, struct MEM *pmem) 83 { 84 PNODE pnew = malloc(sizeof(node)); 85 pnew->pmem = pmem; 86 pnew->pNext = NULL; 87 88 if (phead == NULL) 89 { 90 phead = pnew; 91 } 92 else 93 { 94 PNODE ptemp = phead; 95 while (ptemp->pNext != NULL) 96 { 97 ptemp = ptemp->pNext; 98 } 99 ptemp->pNext = pnew; 100 } 101 return phead; 102 } 103 104 PNODE change(PNODE phead, void *pfind, struct MEM *pnewmem) 105 { 106 for (PNODE p = phead; p != NULL; p = p->pNext) 107 { 108 if (p->pmem->p == pfind) 109 { 110 p->pmem->p = pnewmem->p; 111 p->pmem->size = pnewmem->size; 112 return phead; 113 } 114 } 115 return phead; 116 } 117 118 PNODE deletenode(PNODE phead, void *paddr) 119 { 120 PNODE p1, p2; 121 p1 = p2 = NULL; 122 p1 = phead; 123 while (p1 != NULL) 124 { 125 if (p1->pmem->p == paddr) 126 { 127 break; 128 } 129 else 130 { 131 p2 = p1; 132 p1 = p1->pNext; 133 } 134 } 135 136 if (p1 != NULL) 137 { 138 if (p1 != phead) 139 { 140 p2->pNext = p1->pNext; 141 free(p1); 142 } 143 else 144 { 145 phead = phead->pNext; 146 free(p1); 147 } 148 } 149 150 151 return phead; 152 } 153 void showall(PNODE phead) 154 { 155 if (phead == NULL) 156 { 157 return; 158 } 159 else 160 { 161 printf("memaddr=%p,size=%d\n", phead->pmem->p, phead->pmem->size); 162 showall(phead->pNext); 163 } 164 } 165 166 PNODE findit(PNODE phead, void *pfind) 167 { 168 for (PNODE p = phead; p != NULL; p = p->pNext) 169 { 170 if (p->pmem->p == pfind) 171 { 172 return p; 173 } 174 } 175 return NULL; 176 } 177 178 void getinfo(PNODE phead) 179 { 180 int i = 0;//多少个地址 181 int j = 0;//多少个字节内存 182 for (PNODE p = phead; p != NULL; p = p->pNext) 183 { 184 i++; 185 j += p->pmem->size;; 186 } 187 printf("目前一共%d个地址在使用,一共消耗%d内存\n",i,j); 188 } 189 190 PNODE deleteall(PNODE phead) 191 { 192 PNODE p1, *p2; 193 p1 = NULL; 194 p2 = NULL; 195 if (phead == NULL) 196 { 197 return; 198 } 199 else 200 { 201 p1 = phead; 202 while (p1->pNext != NULL) 203 { 204 p2 = p1->pNext; 205 printf("内存%p被删除\n", p1->pmem->p); 206 free(p1->pmem->p); 207 free(p1); 208 p1 = p2; 209 210 } 211 printf("内存%p被删除\n", p1->pmem->p); 212 free(p1->pmem->p); 213 free(p1); 214 } 215 return NULL; 216 } 217 218 219 //main.c 220 #include "mem.h" 221 #define malloc mymalloc 222 #define free myfree 223 #define realloc myrealloc 224 225 226 void main() 227 { 228 void *p1 = malloc(10); 229 void *p2 = malloc(11); 230 void *p3 = malloc(12); 231 void *p4 = malloc(13); 232 showall(phead); 233 printf("\n\n\n"); 234 p1 = realloc(p1, 100); 235 showall(phead); 236 printf("\n\n\n"); 237 free(p1); 238 showall(phead); 239 printf("\n\n\n"); 240 printf("\n\n\n"); 241 phead = deleteall(phead); 242 showall(phead); 243 system("pause"); 244 }
//指针的副本机制 在函数参数中 如果传递的是指针,当改变的是指针所指向的地址,则改变的是作为参数的指针所指向的内存,原来的指针所指向的地址不变,改变的是这片内存所储存的内容 要改变一个n级指针需要n+1级指针