指針學(xué)習(xí)2--內(nèi)存泄露
內(nèi)存泄露的情形:
1
#include <iostream>
2
using namespace std;
3
4
class Stu
5

{
6
public:
7
Stu(int m):var(m)
8
{
9
cout << var <<" constructor called." << endl;
10
}
11
~Stu()
{ cout << var << " destructor called." << endl;}
12
private:
13
int var;
14
};
15
16
17
int main()
18

{
19
Stu *a = new Stu(20);
20
Stu *b = new Stu(30);
21
delete b;
22
return 0;
23
//or 其他隱藏異常
24
25
//導(dǎo)致內(nèi)存泄露
26
delete a;
27
28
return 0;
29
}

2

3

4

5



6

7

8



9

10

11



12

13

14

15

16

17

18



19

20

21

22

23

24

25

26

27

28

29


posted on 2010-04-18 12:06 幸運(yùn)草 閱讀(575) 評論(0) 編輯 收藏 引用 所屬分類: C++