监控应用程序的内存申请和释放

题目:监控应用程序的内存申请和释放

描述:
不考虑多线程,使用动态单链表的方式进行操作,在应用程序申请内存时,需要把该内存节点信息插入链表进行记录,
应用程序释放内存,把相应内存节点的信息从链表中删除,在应用程序退出时,如果链表仍然存在节点,说明存在
内存泄露,应用程序不存在内存泄露,则链表为空。


请大侠们用C语言编写出内存的申请和释放的封装函数。

MLCmalloc()  封装了malloc ,同时将信息插入链表操作
MLCfree()    封装了free ,同时将信息从链表中删除

 

  1. //by CSYNYK 
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <windows.h>
  5. #define   _MC    1     // MLCmalloc()的标志
  6. #define   _FE    0     // MLCfree()的标志
  7. #define  _Check  -1    //检测内存的标志
  8. typedef struct _Memory
  9. {
  10.     unsigned int address;
  11.     struct _Memory *next;
  12. }Mem, *LinkMem;
  13. int MemoryContrl(size_t addr,char flag)
  14. {
  15.     static Mem head;
  16.     LinkMem tmp=head.next, node=&head ; 
  17.     if( _MC==flag )
  18.     {
  19.         while(tmp)
  20.         {
  21.             node=tmp;
  22.             tmp=tmp->next;
  23.         }
  24.         node->next=(LinkMem)malloc(sizeof(Mem));
  25.         if(node->next==NULL)
  26.             return 1;    //分配失败!
  27.         node=node->next;
  28.         node->address=addr;
  29.         node->next=NULL;
  30.     }
  31.     else if( _FE==flag)
  32.     {
  33.         while(tmp && (tmp->address!=addr))
  34.         {
  35.             node=tmp;
  36.             tmp=tmp->next;
  37.         }
  38.         if(tmp==NULL)
  39.             return -1;    //free地址出错!
  40.         else
  41.         {
  42.             node->next=tmp->next;
  43.             free(tmp);
  44.         }
  45.     }
  46.     else                          
  47.         return (int)( head.next? head.next->address :0 );  //带头结点的链表
  48.     return 0;
  49. }
  50. void *MLCmalloc(size_t size)
  51. {
  52.     void *pMalc=malloc(size);
  53.     if(pMalc==NULL)
  54.         return NULL;
  55.     else if(MemoryContrl((size_t)pMalc, _MC)==1)
  56.     {
  57.         free(pMalc);   //动态链建立失败时释放此次分配的空间
  58.         return NULL;
  59.     }        
  60.     else
  61.         return pMalc;
  62. }
  63. void MLCfree(void *ptr)
  64. {
  65.     if(MemoryContrl((size_t)ptr, _FE)==-1)
  66.     {
  67.         printf("%s/n","被释放的内存地址错误!");
  68.         return ;
  69.     }
  70.     else
  71.         free(ptr);
  72. }
  73. size_t CheckLeak()   //检查泄漏
  74. {
  75.     return (size_t)(MemoryContrl((size_t)0, _Check));
  76. }
  77. int main()
  78. {
  79.     char **p=NULL;
  80.     char *str="1234567890098765432";
  81.     char *tmp,i;
  82.     p=(char **)MLCmalloc(10*sizeof(char**));
  83.     printf("分配p=%#x/n",p);
  84.     for( i=0;i<10; i++)
  85.     {
  86.         p[i]=(char *)MLCmalloc(20*sizeof(char *));
  87.         sscanf_s(str,"%s",p[i]);
  88.     }
  89.     for( i=0; i<10; i++)
  90.         printf("&p[%d]=%#x  *p[%d]=%s/n",i,p[i],i,p[i]);
  91.     printf("释放p=%#x/n",p[2]);            //不安正常顺序释放,让下面的代码来检测泄漏。
  92.     MLCfree(p[2]);
  93.     while((tmp=(char*)CheckLeak())!=0)  //检测是否有泄漏空间
  94.     {
  95.         MLCfree(tmp);               //释放检测到的泄漏空间
  96.         printf("tmp=%#x/n",tmp);
  97.     }
  98.     return 0;
  99. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值