指针学习2--内存泄露
内存泄露的情形
:
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
//导致内存泄露
26
delete a;
27
28
return 0;
29
}
#include <iostream>2
using namespace std; 3

4
class Stu5
{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
//导致内存泄露26
delete a;27

28
return 0;29
}
转载请注明出处: http://www.cnblogs.com/liyuxia713/



浙公网安备 33010602011771号