Memory.txt
上传用户:cfy3119cc
上传日期:2019-05-13
资源大小:1k
文件大小:4k
源码类别:

系统编程

开发平台:

C/C++

  1. 主要是解决自己分配的内存忘记释放的问题,自己定义了几个函数取代了malloc,calloc,realloc,free这几个函数,尽量跟原有用法一致。 
  2. 头文件mypool.h 
  3. #ifndef _MYPOOL_H 
  4. #define _MYPOOL_H 
  5. struct Node 
  6. struct Node *preNode;//前一个节点 
  7. struct Node *nextNode;//后一个节点 
  8. void **varAddr;//存储指针变量的地址 
  9. int size; 
  10. char freed; 
  11. }; 
  12. struct Chain 
  13. struct Node *first; 
  14. struct Node *last; 
  15. int size; 
  16. }; 
  17. void InitChain(); 
  18. struct Node* InitNode(struct Node *pn); 
  19. int Push(struct Node *pn); 
  20. int RemoveChain(void **id); 
  21. int FreeChain(); 
  22. void* MyMalloc(void **p,int size); 
  23. void* MyCalloc(void **p,int nsize,int usize); 
  24. void* MyRealloc(void **p,int size); 
  25. void MyFree(void **p); 
  26. #endif 
  27. 实现代码:mypool.c 
  28. /************************************************************************/ 
  29. /*这些代码主要是实现对自己分配的内存的管理,主要是为了防止在程序关闭后还有忘记释放的内存;*/ 
  30. /*这块代码并不涉及对内存区块的分配管理。*/ 
  31. /* 作者:jackyvan ,Email:jackyyvan@gmail.com */ 
  32. /************************************************************************/ 
  33. #include <stdio.h> 
  34. #include <stdlib.h> 
  35. #include <string.h> 
  36. #include "mypool.h" 
  37. static struct Chain chain;//定义一个链表的静态变量 
  38. /*初始化链表*/ 
  39. void InitChain() 
  40. chain.first=NULL; 
  41. chain.last=NULL; 
  42. chain.size=0; 
  43. /*初始化一个链表上的节点*/ 
  44. struct Node* InitNode(struct Node *pn) 
  45. pn=malloc(sizeof(struct Node)); 
  46. if(pn==NULL) 
  47. return NULL; 
  48. pn->preNode=NULL; 
  49. pn->nextNode=NULL; 
  50. pn->freed=0; 
  51. pn->varAddr=0; 
  52. pn->size=0; 
  53. return pn; 
  54. /*加入一个新的内存分配的节点*/ 
  55. int Push(struct Node *pn) 
  56. struct Node *last=chain.last; 
  57. struct Node *first=chain.first; 
  58. if(first==NULL) 
  59. chain.first=pn; 
  60. chain.last=pn; 
  61. else 
  62. chain.last->nextNode=pn; 
  63. pn->preNode=chain.last; 
  64. chain.last=pn; 
  65. chain.size++; 
  66. return 1; 
  67. /* 
  68. 从链表中移除一个节点 
  69. */ 
  70. int RemoveChain(void **id) 
  71. struct Node *first=chain.first; 
  72. struct Node *tp1=NULL,*tp2=NULL; 
  73. if(first==NULL) 
  74. return 0; 
  75. while(first) 
  76. if((long)first->varAddr==(long)id) 
  77. tp1=first->preNode; 
  78. tp2=first->nextNode; 
  79. if(tp1) 
  80. if(tp2) 
  81. tp1->nextNode=tp2; 
  82. tp2->preNode=tp1; 
  83. else 
  84. tp1->nextNode=NULL; 
  85. chain.last=tp1; 
  86. else 
  87. tp2->preNode=NULL; 
  88. chain.first=tp2; 
  89. free(first); 
  90. chain.size--; 
  91. break; 
  92. first=first->nextNode; 
  93. return 1; 
  94. /*清空链表*/ 
  95. int FreeChain() 
  96. struct Node *first=chain.first; 
  97. struct Node *tp1=NULL; 
  98. while(first) 
  99. tp1=first->nextNode; 
  100. free((void *)*(first->varAddr)); 
  101. free(first); 
  102. first=tp1; 
  103. chain.first=NULL; 
  104. chain.last=NULL; 
  105. chain.size=0; 
  106. return 1; 
  107. /* 
  108. 自定义的malloc,calloc,realloc,free函数 
  109. void **p参数 是存储分配内存地址的变量的地址,根据这个地址与分配内存关联,进行管理 
  110. */ 
  111. void* MyMalloc(void **p,int size) 
  112. struct Node *pn=NULL; 
  113. (*p)=malloc(size); 
  114. if(p==NULL) 
  115. return NULL; 
  116. pn=InitNode(pn); 
  117. if(pn==NULL) 
  118. return NULL; 
  119. pn->varAddr=p; 
  120. pn->size=size; 
  121. Push(pn); 
  122. return (*p); 
  123. void* MyCalloc(void **p,int nsize,int usize) 
  124. struct Node *pn=NULL; 
  125. (*p)=calloc(nsize,usize); 
  126. if(p==NULL) 
  127. return NULL; 
  128. pn=InitNode(pn); 
  129. if(pn==NULL) 
  130. return NULL; 
  131. pn->varAddr=p; 
  132. pn->size=nsize*usize; 
  133. Push(pn); 
  134. return (*p); 
  135. void* MyRealloc(void **p,int size) 
  136. struct Node *pn=NULL; 
  137. (*p)=realloc((*p),size); 
  138. if(p==NULL) 
  139. return NULL; 
  140. pn=InitNode(pn); 
  141. if(pn==NULL) 
  142. return NULL; 
  143. pn->varAddr=p; 
  144. pn->size=size; 
  145. RemoveChain(p); 
  146. Push(pn); 
  147. return (*p); 
  148. void MyFree(void **p) 
  149. if((*p)==NULL) 
  150. return; 
  151. free((*p));//释放内存 
  152. RemoveChain(p);//把相关节点从链表移除 
  153. int main() 
  154. char *p=NULL; 
  155. char *p2=NULL; 
  156. int *p3=NULL; 
  157. InitChain(); 
  158. p=MyCalloc(&p,100,sizeof(char)); 
  159. strcpy(p,"abcdefgh..."); 
  160. p2=MyMalloc(&p2,18*sizeof(char)); 
  161. p3=MyMalloc(&p3,10*sizeof(int)); 
  162. p3=MyRealloc(&p3,20*sizeof(int)); 
  163. MyFree(&p2); 
  164. FreeChain(); 
  165. return 0;