内存管理 二

发现两篇内存池的博文,博文地址如下,写的很好,能避免内存碎片和内存泄露问题,比我这个玩具代码要好很多,大家可以看看:

http://www.cnblogs.com/bangerlee/archive/2011/08/31/2161421.html

http://blog.youkuaiyun.com/060/article/details/1326025


    在我们做项目的时候,经常会分配了内存,然后却忘了释放,造成内存泄漏的问题。

    以下代码可以实现在代码退出的时候自动释放之前申请但未释放的内存。

    其原理是:用一个双向链表维护申请的内存块,申请内存则插入对应节点,释放则删除相应节点;当程序退出的时候,遍历双向链表,释放内存,清空双向链表。

    代码如下:

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4.   
  5. struct Node  
  6. {  
  7.  struct Node *preNode;//前一个节点  
  8.  struct Node *nextNode;//后一个节点  
  9.  void **varAddr;//存储指针变量的地址  
  10.  int size;  
  11.  char freed;  
  12. };  
  13.   
  14. struct Chain  
  15. {  
  16.  struct Node *first;  
  17.  struct Node *last;  
  18.  int size;  
  19. };  
  20. void InitChain();  
  21. struct Node* InitNode(struct Node *pn);  
  22. int Push(struct Node *pn);  
  23. int RemoveChain(void **id);  
  24. int FreeChain();  
  25.   
  26.   
  27. void* MyMalloc(void **p,int size);  
  28. void* MyCalloc(void **p,int nsize,int usize);  
  29. void* MyRealloc(void **p,int size);  
  30. void MyFree(void **p);  
  31.   
  32. static struct Chain chain;//定义一个链表的静态变量  
  33.   
  34.   
  35. /*初始化链表*/  
  36. void InitChain()  
  37. {  
  38.  chain.first=NULL;  
  39.  chain.last=NULL;  
  40.  chain.size=0;  
  41. }  
  42. /*初始化一个链表上的节点*/  
  43. struct Node* InitNode(struct Node *pn)  
  44. {  
  45.  pn=(struct Node *)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. /*加入一个新的内存分配的节点*/  
  56. int Push(struct Node *pn)  
  57. {  
  58.  struct Node *last=chain.last;  
  59.  struct Node *first=chain.first;  
  60.  if(first==NULL)  
  61.  {  
  62.   chain.first=pn;  
  63.   chain.last=pn;  
  64.  }  
  65.  else  
  66.  {  
  67.   chain.last->nextNode=pn;  
  68.   pn->preNode=chain.last;  
  69.   chain.last=pn;  
  70.  }  
  71.  chain.size++;  
  72.  return 1;  
  73. }  
  74. /* 
  75. 从链表中移除一个节点 
  76. */  
  77. int RemoveChain(void **id)  
  78. {  
  79.  struct Node *first=chain.first;  
  80.  struct Node *tp1=NULL,*tp2=NULL;  
  81.  if(first==NULL)  
  82.   return 0;  
  83.  while(first)  
  84.  {  
  85.   
  86.   if((long)first->varAddr==(long)id)  
  87.   {  
  88.    tp1=first->preNode;  
  89.    tp2=first->nextNode;  
  90.    if(tp1)  
  91.    {  
  92.     if(tp2)  
  93.     {  
  94.      tp1->nextNode=tp2;  
  95.      tp2->preNode=tp1;  
  96.     }  
  97.     else  
  98.     {  
  99.      tp1->nextNode=NULL;  
  100.      chain.last=tp1;  
  101.     }  
  102.    }  
  103.    else  
  104.    {  
  105.     tp2->preNode=NULL;  
  106.     chain.first=tp2;  
  107.    }  
  108.    free(first);  
  109.    chain.size--;  
  110.    break;  
  111.   }  
  112.   first=first->nextNode;  
  113.  }  
  114.  return 1;  
  115. }  
  116. /*清空链表*/  
  117. int FreeChain()  
  118. {  
  119.  struct Node *first=chain.first;  
  120.  struct Node *tp1=NULL;  
  121.  while(first)  
  122.  {  
  123.   tp1=first->nextNode;  
  124.   free((void *)*(first->varAddr));  
  125.   free(first);  
  126.   first=tp1;  
  127.  }  
  128.  chain.first=NULL;  
  129.  chain.last=NULL;  
  130.  chain.size=0;  
  131.  return 1;  
  132. }  
  133. /* 
  134. 自定义的malloc,calloc,realloc,free函数 
  135. void **p参数 是存储分配内存地址的变量的地址,根据这个地址与分配内存关联,进行管理 
  136. */  
  137. void* MyMalloc(void **p,int size)  
  138. {  
  139.  struct Node *pn=NULL;  
  140.  (*p)=malloc(size);  
  141.  if(p==NULL)  
  142.   return NULL;  
  143.  pn=InitNode(pn);  
  144.  if(pn==NULL)  
  145.   return NULL;  
  146.  pn->varAddr=p;  
  147.  pn->size=size;  
  148.  Push(pn);  
  149.  return (*p);  
  150. }  
  151. void* MyCalloc(void **p,int nsize,int usize)  
  152. {  
  153.  struct Node *pn=NULL;  
  154.  (*p)=calloc(nsize,usize);  
  155.  if(p==NULL)  
  156.   return NULL;  
  157.  pn=InitNode(pn);  
  158.  if(pn==NULL)  
  159.   return NULL;  
  160.  pn->varAddr=p;  
  161.  pn->size=nsize*usize;  
  162.  Push(pn);  
  163.  return (*p);  
  164. }  
  165. void* MyRealloc(void **p,int size)  
  166. {  
  167.  struct Node *pn=NULL;  
  168.  (*p)=realloc((*p),size);  
  169.  if(p==NULL)  
  170.   return NULL;  
  171.  pn=InitNode(pn);  
  172.  if(pn==NULL)  
  173.   return NULL;  
  174.  pn->varAddr=p;  
  175.  pn->size=size;  
  176.  RemoveChain(p);  
  177.  Push(pn);  
  178.  return (*p);  
  179. }  
  180.   
  181. void MyFree(void **p)  
  182. {  
  183.  if((*p)==NULL)  
  184.   return;  
  185.  free((*p));//释放内存  
  186.  RemoveChain(p);//把相关节点从链表移除  
  187. }  
  188.   
  189.   
  190. int main()  
  191. {  
  192.  char *p=NULL;  
  193.  char *p2=NULL;  
  194.  int *p3=NULL;  
  195.  InitChain();  
  196.  p=(char *)MyCalloc((void **)&p,100,sizeof(char));  
  197.  strcpy(p,"abcdefgh...");  
  198.  p2=(char *)MyMalloc((void **)&p2,18*sizeof(char));  
  199.  p3=(int *)MyMalloc((void **)&p3,10*sizeof(int));  
  200.  p3=(int *)MyRealloc((void **)&p3,20*sizeof(int));  
  201.  MyFree((void **)&p2);  
  202.  FreeChain();  
  203.  return 0;  
  204. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值