内存管理工具

c++中动态申请的内存地址放入到内存链表中,重载了全局new,new[],delete,delete[],用来检测是否重复释放内存、是否有未释放的内存、自动释放未释放的内存。

  1 #ifndef _MEMLIST_H_
  2 #define _MEMLIST_H_ 
  3 
  4 #include <iostream>
  5 using namespace std;
  6 
  7 class MemListNode
  8 {
  9 public:
 10     friend class MemList;
 11     MemListNode();
 12     ~MemListNode();
 13 private:
 14     void *p;
 15     MemListNode *pNext;
 16 };
 17 
 18 class MemList
 19 {
 20 public:
 21     MemList();
 22     ~MemList();
 23     void InsertChild(void *p);
 24     void Delete(void *pDel);
 25     bool IsEmpty( );
 26     bool Find(void *pFind);
 27     void Print();
 28     void Clear();        //释放掉用户未释放的内存
 29     void Destroy();
 30 private:
 31     MemListNode *pList;
 32 };
 33 #endif
 34 
 35 实现文件:
 36 #include "MemList.h"
 37 
 38 //全局链表
 39 MemList pList;
 40 
 41 MemListNode::MemListNode()
 42 {
 43     p = NULL;
 44     pNext = NULL;
 45 }
 46 
 47 MemListNode::~MemListNode()
 48 {
 49     free (p);
 50     free (pNext);
 51 };
 52 
 53 MemList::MemList()
 54 {
 55     pList = (MemListNode *)malloc(sizeof(MemListNode));
 56     pList->p = NULL;
 57     pList->pNext = NULL;
 58 }
 59 
 60 MemList::~MemList()
 61 {
 62     Destroy();
 63 }
 64 
 65 void MemList::InsertChild(void *p)
 66 {
 67     if (!p)
 68     {
 69         return;
 70     }
 71     MemListNode *pTmp = (MemListNode *)malloc(sizeof(MemListNode));
 72     pTmp->p = p;
 73     pTmp->pNext = NULL;
 74 
 75     pTmp->pNext = pList->pNext;
 76     pList->pNext = pTmp;
 77 }
 78 
 79 void MemList::Delete(void *pDel)
 80 {
 81     if (!Find(pDel))
 82     {
 83         return;
 84     }
 85     MemListNode *pCur = pList;
 86     while(pCur->pNext)
 87     {
 88         if(pCur->pNext->p == pDel)
 89         {
 90             MemListNode *pTmp = pCur->pNext;
 91             pCur->pNext = pTmp->pNext;
 92             free(pTmp);
 93         }
 94         else
 95         {
 96             pCur = pCur->pNext;
 97         }
 98     }
 99 }
100 
101 bool MemList::Find(void *pFind)
102 {
103     if (NULL == pFind)
104     {
105         return false;
106     }
107     MemListNode *pCur = pList->pNext;
108     while(pCur)
109     {
110         if (pCur->p == pFind)
111         {
112             return true;
113         }
114         pCur = pCur->pNext;
115     }
116 
117     return false;
118 }
119 
120 bool MemList::IsEmpty()
121 {
122     return pList->pNext == NULL;
123 }
124 
125 void MemList::Print()
126 {
127     MemListNode *pCur = pList->pNext;
128     while(pCur)
129     {
130         cout<<pCur->p<<endl;
131         pCur = pCur->pNext;
132     }
133 }
134 
135 void MemList::Clear()
136 {
137     if (IsEmpty())
138     {
139         return;
140     }
141     MemListNode *pCur = pList->pNext;
142     while(pCur)
143     {
144         if (pCur->p != NULL)
145         {
146             free(pCur->p);
147         }
148         pCur = pCur->pNext;
149     }
150 }
151 void MemList::Destroy()
152 {
153     while(pList->pNext)
154     {
155         MemListNode *pTmp = pList->pNext;
156         pList->pNext = pTmp->pNext;
157         free(pTmp);
158     }
159 
160     free(pList);
161 }
162 
163 //重载全局new
164 void* operator new (size_t size)
165 {
166     void *p =malloc(size);
167     pList.InsertChild(p);
168 
169     return p;
170 }
171 
172 //重载全局new[]
173 void* operator new[] (size_t size)
174 {
175     return operator new(size);
176 }
177 
178 //重载全局delete
179 void operator delete (void *p)
180 {
181     if (pList.Find(p))
182     {
183         pList.Delete(p);
184         free(p);
185         p = NULL;
186     }
187     else
188     {
189         cout<<"重复释放内存"<<endl;
190     }
191 }
192 
193 //重载全局delete[]
194 void operator delete[] (void *p)
195 {
196     delete(p);
197 }
198 
199 测试用例:
200 #include <iostream>
201 #include "MemList.h"
202 using namespace std;
203 extern MemList pList;
204 
205 class Point
206 {
207 public:
208     Point(int a = 0, int b = 0):m_x(a), m_y(b)
209     { 
210         cout<<"构造函数"<<endl;
211     }
212     ~Point()
213     { 
214         cout<<"析构函数"<<endl;
215     }
216     void SetValue(int a, int b)
217     {
218         m_x = a;
219         m_y = b;
220     }
221 private:
222     int m_x;
223     int m_y;
224 };
225 
226 int main()
227 {
228     Point *pTest = new Point[2];
229     pTest[0].SetValue(1, 2);
230     pTest[1].SetValue(10, 20);
231 
232     delete[] pTest;
233 
234     int *pInt = new int(12);
235     double *pDouble = new double(10.0);
236     delete pDouble;
237     delete pDouble;
238     if (!pList.IsEmpty())
239     {
240         cout<<"内存未释放完全,我帮你释放掉吧!"<<endl;
241         pList.Clear();
242     }
243 
244     system("pause");
245     return 0;
246 }

 

 

posted @ 2018-03-06 09:01  NoThx  阅读(299)  评论(0编辑  收藏  举报