赞
踩
目录
三.operator new与operator delete函数
五.定位new表达式(placement-new)(构造函数)
自定义类型调用构造函数和析构函数
抛异常用try/catch
1.内置类型:
申请的是内置类型的空间,new和malloc,delete和free基本类似,不同的地方是:new/delete申请和 释放的是单个元素的空间,new[]和delete[]申请的是连续空间,而且new在申请空间失败时会抛异常, malloc会返回NULL
- #include"iostream"
- using namespace std;
- int main()
- {
-
- int* p1 = new int;
- free(p1);//内置类型用free与new配套不会造成内存泄漏
-
- return 0;
- }
- #include"iostream"
- using namespace std;
- class A
- {
- public:
- A(int a1 = 0, int a2 = 0)
- :_a1(a1)
- , _a2(a2)
- {
- cout << "A(int a1 = 0, int a2 = 0)" << endl;
- }
-
- A(const A& aa)
- :_a1(aa._a1)
- {
- cout << "A(const A& aa)" << endl;
- }
-
- A& operator=(const A& aa)
- {
- cout << "A& operator=(const A& aa)" << endl;
- if (this != &aa)
- {
- _a1 = aa._a1;
- }
- return *this;
- }
-
- ~A()
- {
- //delete _ptr;
- cout << "~A()" << endl;
- }
-
- void Print()
- {
- cout << "A::Print->" << _a1 << endl;
- }
-
- A& operator++()
- {
- _a1 += 100;
-
- return *this;
- }
- private:
- int _a1 = 1;
- int _a2 = 1;
- };
- int main()
- {
-
- A* p1 = new A;
- free(p1);//自定义类型,析构函数中没有释放资源,也不会造成内存泄漏
-
- return 0;
- }
- #include"iostream"
- using namespace std;
- int main()
- {
-
-
- int* p1 = new int[10];
- delete p1;//不报错
-
-
- return 0;
- }
- class A
- {
- public:
- A(int a1 = 0, int a2 = 0)
- :_a1(a1)
- , _a2(a2)
- {
- cout << "A(int a1 = 0, int a2 = 0)" << endl;
- }
-
- A(const A& aa)
- :_a1(aa._a1)
- {
- cout << "A(const A& aa)" << endl;
- }
-
- A& operator=(const A& aa)
- {
- cout << "A& operator=(const A& aa)" << endl;
- if (this != &aa)
- {
- _a1 = aa._a1;
- }
- return *this;
- }
-
- ~A()
- {
- //delete _ptr;
- cout << "~A()" << endl;
- }
-
- void Print()
- {
- cout << "A::Print->" << _a1 << endl;
- }
-
- A& operator++()
- {
- _a1 += 100;
-
- return *this;
- }
- private:
- int _a1 = 1;
- int _a2 = 1;
- };
-
- class B
- {
- public:
- /*~B()
- {
- cout << "~B()" << endl;
- }*/
- private:
- int _b1 = 2;
- int _b2 = 2;
- };
-
- #include"iostream"
- using namespace std;
- int main()
- {
-
- A* p1 = new A[10];
- delete p1;//由于A类有析构函数,所以编译器会开四个字节存放要析构的个数,但是仅释放80个字节的空间,故由于不能从中间释放,所以系统崩溃
-
- B* p2 = new B[10];
- delete p2;//由于B类没有析构函数,不会再开头开4个字节的空间,故从头释放,不会崩溃
- return 0;
- }
- class A
- {
- public:
- A(int a1 = 0, int a2 = 0)
- :_a1(a1)
- , _a2(a2)
- {
- cout << "A(int a1 = 0, int a2 = 0)" << endl;
- }
-
- A(const A& aa)
- :_a1(aa._a1)
- {
- cout << "A(const A& aa)" << endl;
- }
-
- A& operator=(const A& aa)
- {
- cout << "A& operator=(const A& aa)" << endl;
- if (this != &aa)
- {
- _a1 = aa._a1;
- }
- return *this;
- }
-
- ~A()
- {
- //delete _ptr;
- cout << "~A()" << endl;
- }
-
- void Print()
- {
- cout << "A::Print->" << _a1 << endl;
- }
-
- A& operator++()
- {
- _a1 += 100;
-
- return *this;
- }
- private:
- int _a1 = 1;
- int _a2 = 1;
- };
-
- #include"iostream"
- using namespace std;
- int main()
- {
- A* p1 = new A(1);
- delete p1;
-
- A* p2 = (A*)operator new(sizeof(A));
- new(p2)A(1);
- p2->~A();
- operator delete(p2);
-
-
- return 0;
- }
内存池(池化技术--》提高性能) 高频申请释放内存块(仅申请了空间,要调用operator new初始化)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。