生活随笔
收集整理的這篇文章主要介紹了
内存管理 二
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
發現兩篇內存池的博文,博文地址如下,寫的很好,能避免內存碎片和內存泄露問題,比我這個玩具代碼要好很多,大家可以看看:
http://www.cnblogs.com/bangerlee/archive/2011/08/31/2161421.html
http://blog.csdn.net/060/article/details/1326025
? ? 在我們做項目的時候,經常會分配了內存,然后卻忘了釋放,造成內存泄漏的問題。
? ? 以下代碼可以實現在代碼退出的時候自動釋放之前申請但未釋放的內存。
? ? 其原理是:用一個雙向鏈表維護申請的內存塊,申請內存則插入對應節點,釋放則刪除相應節點;當程序退出的時候,遍歷雙向鏈表,釋放內存,清空雙向鏈表。
? ? 代碼如下:
[cpp]?view plaincopy
#include?<stdio.h>?? #include?<stdlib.h>?? #include?<string.h>?? ?? struct?Node?? {?? ?struct?Node?*preNode;?? ?struct?Node?*nextNode;?? ?void?**varAddr;?? ?int?size;?? ?char?freed;?? };?? ?? struct?Chain?? {?? ?struct?Node?*first;?? ?struct?Node?*last;?? ?int?size;?? };?? void?InitChain();?? struct?Node*?InitNode(struct?Node?*pn);?? int?Push(struct?Node?*pn);?? int?RemoveChain(void?**id);?? int?FreeChain();?? ?? ?? void*?MyMalloc(void?**p,int?size);?? void*?MyCalloc(void?**p,int?nsize,int?usize);?? void*?MyRealloc(void?**p,int?size);?? void?MyFree(void?**p);?? ?? static?struct?Chain?chain;?? ?? ?? ?? void?InitChain()?? {?? ?chain.first=NULL;?? ?chain.last=NULL;?? ?chain.size=0;?? }?? ?? struct?Node*?InitNode(struct?Node?*pn)?? {?? ?pn=(struct?Node?*)malloc(sizeof(struct?Node));?? ?if(pn==NULL)?? ??return?NULL;?? ?pn->preNode=NULL;?? ?pn->nextNode=NULL;?? ?pn->freed=0;?? ?pn->varAddr=0;?? ?pn->size=0;?? ?return?pn;?? }?? ?? int?Push(struct?Node?*pn)?? {?? ?struct?Node?*last=chain.last;?? ?struct?Node?*first=chain.first;?? ?if(first==NULL)?? ?{?? ??chain.first=pn;?? ??chain.last=pn;?? ?}?? ?else?? ?{?? ??chain.last->nextNode=pn;?? ??pn->preNode=chain.last;?? ??chain.last=pn;?? ?}?? ?chain.size++;?? ?return?1;?? }?? ? ? ?? int?RemoveChain(void?**id)?? {?? ?struct?Node?*first=chain.first;?? ?struct?Node?*tp1=NULL,*tp2=NULL;?? ?if(first==NULL)?? ??return?0;?? ?while(first)?? ?{?? ?? ??if((long)first->varAddr==(long)id)?? ??{?? ???tp1=first->preNode;?? ???tp2=first->nextNode;?? ???if(tp1)?? ???{?? ????if(tp2)?? ????{?? ?????tp1->nextNode=tp2;?? ?????tp2->preNode=tp1;?? ????}?? ????else?? ????{?? ?????tp1->nextNode=NULL;?? ?????chain.last=tp1;?? ????}?? ???}?? ???else?? ???{?? ????tp2->preNode=NULL;?? ????chain.first=tp2;?? ???}?? ???free(first);?? ???chain.size--;?? ???break;?? ??}?? ??first=first->nextNode;?? ?}?? ?return?1;?? }?? ?? int?FreeChain()?? {?? ?struct?Node?*first=chain.first;?? ?struct?Node?*tp1=NULL;?? ?while(first)?? ?{?? ??tp1=first->nextNode;?? ??free((void?*)*(first->varAddr));?? ??free(first);?? ??first=tp1;?? ?}?? ?chain.first=NULL;?? ?chain.last=NULL;?? ?chain.size=0;?? ?return?1;?? }?? ? ? ? ?? void*?MyMalloc(void?**p,int?size)?? {?? ?struct?Node?*pn=NULL;?? ?(*p)=malloc(size);?? ?if(p==NULL)?? ??return?NULL;?? ?pn=InitNode(pn);?? ?if(pn==NULL)?? ??return?NULL;?? ?pn->varAddr=p;?? ?pn->size=size;?? ?Push(pn);?? ?return?(*p);?? }?? void*?MyCalloc(void?**p,int?nsize,int?usize)?? {?? ?struct?Node?*pn=NULL;?? ?(*p)=calloc(nsize,usize);?? ?if(p==NULL)?? ??return?NULL;?? ?pn=InitNode(pn);?? ?if(pn==NULL)?? ??return?NULL;?? ?pn->varAddr=p;?? ?pn->size=nsize*usize;?? ?Push(pn);?? ?return?(*p);?? }?? void*?MyRealloc(void?**p,int?size)?? {?? ?struct?Node?*pn=NULL;?? ?(*p)=realloc((*p),size);?? ?if(p==NULL)?? ??return?NULL;?? ?pn=InitNode(pn);?? ?if(pn==NULL)?? ??return?NULL;?? ?pn->varAddr=p;?? ?pn->size=size;?? ?RemoveChain(p);?? ?Push(pn);?? ?return?(*p);?? }?? ?? void?MyFree(void?**p)?? {?? ?if((*p)==NULL)?? ??return;?? ?free((*p));?? ?RemoveChain(p);?? }?? ?? ?? int?main()?? {?? ?char?*p=NULL;?? ?char?*p2=NULL;?? ?int?*p3=NULL;?? ?InitChain();?? ?p=(char?*)MyCalloc((void?**)&p,100,sizeof(char));?? ?strcpy(p,"abcdefgh...");?? ?p2=(char?*)MyMalloc((void?**)&p2,18*sizeof(char));?? ?p3=(int?*)MyMalloc((void?**)&p3,10*sizeof(int));?? ?p3=(int?*)MyRealloc((void?**)&p3,20*sizeof(int));?? ?MyFree((void?**)&p2);?? ?FreeChain();?? ?return?0;?? } ?
總結
以上是生活随笔為你收集整理的内存管理 二的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。