从一个小程序明白new和delete的奇特现象

#include <iostream>
#include <list>
using namespace std;

class emp
{
		protected:
		string name;
		public:
		emp(){}
		virtual ~emp(){}
		virtual void print() = 0;
		
};

class empch:public emp
{
		
		public:
		empch(const string& str)
		{
				name = str;
		}
		void print()
		{
					cout<<name<<endl;
		}
		
};

int main()
{
		list<emp*> link;
		emp* p=NULL;
		string str = "nihao";
		
		p=new empch(str);
		link.push_back(p);
		delete p;
		p=NULL;
		
		p=new empch(str);
		link.push_back(p);
		delete p;
		p=NULL;
p=new empch(str); link.push_back(p); delete p; p=NULL;
list<emp*>::iterator q = link.begin(); while(q!=link.end())          //执行到此会出现段错误 { (*q)->print(); q++; } return 0; }

 删除最后一个p,运行:

 1 #include <iostream>
 2 #include <list>
 3 using namespace std;
 4 
 5 class emp
 6 {
 7         protected:
 8         string name;
 9         public:
10         emp(){}
11         virtual ~emp(){}
12         virtual void print() = 0;
13         
14 };
15 
16 class empch:public emp
17 {
18         
19         public:
20         empch(const string& str)
21         {
22                 name = str;
23         }
24         void print()
25         {
26                     cout<<name<<endl;
27         }
28         
29 };
30 
31 int main()
32 {
33         list<emp*> link;
34         emp* p=NULL;
35         string str = "nihao";
36         
37         p=new empch(str);
38         link.push_back(p);
39         delete p;
40         p=NULL;
41         
42         p=new empch(str);
43         link.push_back(p);
44         delete p;
45         p=NULL;
46         
47         p=new empch(str);
48         link.push_back(p);
49     //    delete p;        //在此删除delete动作
50         p=NULL;
51         
52         list<emp*>::iterator q = link.begin();
53 
54         while(q!=link.end())
55         {
56                 (*q)->print();58                 q++;
59         }        
60         
61         return 0;
62 }

此时输出:

nihao
nihao
nihao


------------------
(program exited with code: 0)
Press return to continue

修改程序,将容器中保存的各个地址输出:

 1 #include <iostream>
 2 #include <list>
 3 using namespace std;
 4 
 5 class emp
 6 {
 7         protected:
 8         string name;
 9         public:
10         emp(){}
11         virtual ~emp(){}
12         virtual void print() = 0;
13         
14 };
15 
16 class empch:public emp
17 {
18         
19         public:
20         empch(const string& str)
21         {
22                 name = str;
23         }
24         void print()
25         {
26                     cout<<name<<endl;
27         }
28         
29 };
30 
31 int main()
32 {
33         list<emp*> link;
34         emp* p=NULL;
35         string str = "nihao";
36         
37         p=new empch(str);
38         link.push_back(p);
39         delete p;
40         p=NULL;
41         
42         p=new empch(str);
43         link.push_back(p);
44         delete p;
45         p=NULL;
46         
47         p=new empch(str);
48         link.push_back(p);
49         p=NULL;
50         
51         list<emp*>::iterator q = link.begin();
52 
53         while(q!=link.end())
54         {
55                 (*q)->print();
56                 cout<<*q<<endl;
57                 q++;
58         }        
59         
60         return 0;
61 }

输出如下:

nihao
0x9ff4020
nihao
0x9ff4020
nihao
0x9ff4020


------------------
(program exited with code: 0)
Press return to continue

此时发现每个地址都是0x9ff4020,至此,我们应该就明白为什么会发生这样奇怪的情况了。那么到这里,问题又来了,为什么会出现这么个诡异的现象呢?

 

posted @ 2015-06-04 14:32  道心不可练  阅读(909)  评论(0编辑  收藏  举报