작은 프로그램 에서 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 이라는 것 을 발견 하면 우 리 는 왜 이런 이상 한 상황 이 발생 했 는 지 알 수 있 을 것 이다.그렇다면 여기까지 문제 가 또 생 겼 다. 왜 이런 기괴 한 현상 이 나 타 났 을 까?
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
umpy - 행, 열, 요소 값 삭제하기numpy에서는 np.delete()를 사용하여 배열의 행, 열, 값을 삭제할 수 있다. np.delete (배열, 인덱스, axis) 설명을 위하여 먼저 다음과 같이 5x4 배열을 만들어 놓았다. (1) 배열에서 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.