작은 프로그램 에서 new 와 delete 의 특이 한 현상 을 알 수 있 습 니 다.

14827 단어 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

이때 모든 주소 가 0x9ff 4020 이라는 것 을 발견 하면 우 리 는 왜 이런 이상 한 상황 이 발생 했 는 지 알 수 있 을 것 이다.그렇다면 여기까지 문제 가 또 생 겼 다. 왜 이런 기괴 한 현상 이 나 타 났 을 까?

좋은 웹페이지 즐겨찾기