标签:
从用户使用程序的角度来看,内存泄漏本身不会产生什么危害,作为一般的用户,根本感觉不到内存泄漏的存在。真正有危害的是内存泄漏的堆积,这会最终消耗尽系统所有的内存。主要有以下几种表现形式:
在我们写程序的时候,一般会使用malloc,realloc,new等函数从堆中分配到一块内存,使用完后,程序必须负责相应的调用free或delete释放该内存块,否则,这块内存就不能被再次使用,我们就说这块内存泄漏了。如果要避免这个问题,还是要从代码上入手,良好的编码习惯和规范,是避免错误的不二法门。
第一:良好的编码习惯,尽量在涉及内存的程序段,检测出内存泄露。当程式稳定之后,在来检测内存泄露时,无疑增加了排除的困难和复杂度。使用了内存分配的函数,一旦使用完毕,要记得要使用其相应的函数释放掉。
Heap memory:
malloc\realloc ------ free
new \new[] ---------- delete \delete[]
GlobalAlloc------------GlobalFree
要特别注意数组对象的内存泄漏
MyPointEX *pointArray =new MyPointEX [100];
其删除形式为:
delete []pointArray ;
第二:将分配的内存的指针以链表的形式自行管理,使用完毕之后从链表中删除,程序结束时可检查改链表。
第三:Boost 中的smart pointer。
第四:一些常见的工具插件,如ccmalloc、Dmalloc、Leaky等等。
我主要想结合代码讲讲第二个方法,设计思想其实很简单,用到STL中的list。可能有人要问,为什么不用vector呢?
list和vector的区别如下:
vector为存储的对象分配一块连续的地址空间,因此对vector中的元素随机访问效率很高。在vecotor中插入或者删除某个元素,需要将现有元素进行复制,移动。如果vector中存储的对象很大,或者构造函数复杂,则在对现有元素进行拷贝时开销较大,因为拷贝对象要调用拷贝构造函数。对于简单的小对象,vector的效率优于list。vector在每次扩张容量的时候,将容量扩展2倍,这样对于小对象来说,效率是很高的。
list中的对象是离散存储的,随机访问某个元素需要遍历list。在list中插入元素,尤其是在首尾插入元素,效率很高,
只需要改变元素的指针。
vector适用:对象数量变化少,简单对象,随机访问元素频繁
list适用: 对象数量变化大,对象复杂,插入和删除频繁
1 #include <iostream>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <list>
6
7 using namespace std;
8
9 const int nMaxSize = 26;
10 struct Node
11 {
12 int count;
13 Node *next[nMaxSize];
14 };
15 Node *root = NULL;
16 list <Node *> nodeMemory;
17
18 void TreeCreate()
19 {
20 root = (Node *)malloc(sizeof(Node));
21 for (int i = 0; i < nMaxSize; ++i)
22 {
23 root->next[i] = NULL;
24 }
25 }
26
27 void TreeInsert(char *pStr)
28 {
29 int i, j, len = strlen(pStr);
30 Node *p = root;
31 Node *q = NULL;
32 for (i = 0; i < len; ++i)
33 {
34 int id = pStr[i] - ‘a‘;
35 if (p->next[id] == NULL)
36 {
37 q = (Node *)malloc(sizeof(Node));
38 if(q != NULL)
39 nodeMemory.push_back(q);
40 q->count = 0;
41 for (j = 0; j < nMaxSize; ++j)
42 {
43 q->next[j] = NULL;
44 }
45 p->next[id] = q;
46 }
47 p->next[id]->count++;
48 p = p->next[id];
49 }
50 }
51
52 int TreeQuery(char *pStr)
53 {
54 int i, len = strlen(pStr);
55 int id = 0;
56 Node *p = root;
57 for (i = 0; i < len; ++i)
58 {
59 id = pStr[i] - ‘a‘;
60 p = p->next[id];
61 if (p == NULL) return 0;
62 }
63 return p->count;
64 }
65
66 void TreeDelete(Node *p)
67 {
68 if (p == NULL)
69 return ;
70 for (int i = 0; i < nMaxSize; ++i)
71 {
72 TreeDelete(p->next[i]);
73 }
74 nodeMemory.remove(p);
75 free(p);
76 p = NULL;
77 }
78
79 int main(int argc, char **argv)
80 {
81 char szBuffer[16];
82 int res = 0;
83 TreeCreate();
84 int n = 3;
85 while (n)
86 {
87 gets(szBuffer);
88 if (strlen(szBuffer) == 0)
89 break;
90 TreeInsert(szBuffer);
91 n--;
92 }
93 /* scanf("%s", szBuffer);
94 res = TreeQuery(szBuffer);
95 printf("%d\n", res); */
96 for(list<Node *>::iterator it = nodeMemory.begin();it != nodeMemory.end();it++)
97 {
98 cout<<*it<<endl;
99 }
100 cout<<nodeMemory.size()<<endl;
101 TreeDelete(root);
102 if(nodeMemory.empty())
103 cout<<"has delete the tire tree"<<endl;
104 cout<<nodeMemory.size()<<endl;
105 system("pause");
106 return 0;
107 }
代码很简单,list存储的是指向内存空间的指针,每次malloc之后会把这个分配的内存指针push到list中,而当free之后,list的就会删除相应的内容,如果全释放掉了,则list变为空,从而可以判断使用后的内存是否全部释放掉了?
第一次在博客园发博客,之前为了方便在sina写博客,发现sina不是一般得差,所以搬到这儿来写了,新的开始,希望大家不吝赐教。
博客参考链接:
http://blog.csdn.net/na_he/article/details/7429171
http://baike.baidu.com/view/1068433.htm
http://blog.csdn.net/renkaihao/article/details/6803866
标签:
原文地址:http://www.cnblogs.com/shouce/p/5476461.html