c++ 11中放弃使用了auto_ptr ;
unique_ptr:
unique_ptr“唯一”拥有其所指对象,同一时刻只能有一个unique_ptr指向给定对象(通过禁止拷贝语义、只有移动语义来实现)。相比与原始指针unique_ptr用于其RAII的特性,使得在出现异常的情况下,动态资源能得到释放。unique_ptr指针本身的生命周期:从unique_ptr指针创建时开始,直到离开作用域。离开作用域时,若其指向对象,则将其所指对象销毁(默认使用delete操作符,用户可指定其他操作)。unique_ptr指针与其所指对象的关系:在智能指针生命周期内,可以改变智能指针所指对象,如创建智能指针时通过构造函数指定、通过reset方法重新指定、通过release方法释放所有权、通过move移动语义转移所有权。
例:
#include <iostream> #include <memory> int main() { { std::unique_ptr<int> uptr(new int(10)); //绑定动态对象 //std::unique_ptr<int> uptr2 = uptr; //不能賦值 //std::unique_ptr<int> uptr2(uptr); //不能拷貝 std::unique_ptr<int> uptr2 = std::move(uptr); //轉換所有權 uptr2.release(); //释放所有权 } //超過uptr的作用域,內存釋放 }
shared_ptr:
shared_ptr多个指针指向相同的对象。shared_ptr使用引用计数,每一个shared_ptr的拷贝都指向相同的内存。每使用他一次,内部的引用计数加1,每析构一次,内部的引用计数减1,减为0时,自动删除所指向的堆内存。shared_ptr内部的引用计数是线程安全的,但是对象的读取需要加锁。
use_count 返回引用计数的个数
unique 返回是否是独占所有权( use_count 为 1)
swap 交换两个 shared_ptr 对象(即交换所拥有的对象)
reset 放弃内部对象的所有权或拥有对象的变更, 会引起原有对象的引用计数的减少
get 返回内部对象(指针), 由于已经重载了()方法, 因此和直接使用对象是一样的
例:
#include <iostream> #include <memory> int main() { { int a = 10; std::shared_ptr<int> ptra = std::make_shared<int>(a); std::shared_ptr<int> ptra2(ptra); //copy std::cout << ptra.use_count() << std::endl; int b = 20; int *pb = &a; //std::shared_ptr<int> ptrb = pb; //error std::shared_ptr<int> ptrb = std::make_shared<int>(b); ptra2 = ptrb; //assign pb = ptrb.get(); //获取原始指针 std::cout << ptra.use_count() << std::endl; std::cout << ptrb.use_count() << std::endl; } }
weak_ptr:
weak_ptr是为了配合shared_ptr而引入的一种智能指针,因为它不具有普通指针的行为,没有重载operator*和->,它的最大作用在于协助shared_ptr工作,像旁观者那样观测资源的使用情况。weak_ptr可以从一个shared_ptr或者另一个weak_ptr对象构造,获得资源的观测权。但weak_ptr没有共享资源,它的构造不会引起指针引用计数的增加。使用weak_ptr的成员函数use_count()可以观测资源的引用计数,另一个成员函数expired()的功能等价于use_count()==0,但更快,表示被观测的资源(也就是shared_ptr的管理的资源)已经不复存在。weak_ptr可以使用一个非常重要的成员函数lock()从被观测的shared_ptr获得一个可用的shared_ptr对象, 从而操作资源。但当expired()==true的时候,lock()函数将返回一个存储空指针的shared_ptr。
例:
#include <iostream> #include <memory> int main() { { std::shared_ptr<int> sh_ptr = std::make_shared<int>(10); std::cout << sh_ptr.use_count() << std::endl; std::weak_ptr<int> wp(sh_ptr); std::cout << wp.use_count() << std::endl; if(!wp.expired()){ std::shared_ptr<int> sh_ptr2 = wp.lock(); //get another shared_ptr *sh_ptr = 100; std::cout << wp.use_count() << std::endl; } } //delete memory }
案例:循环引用
考虑一个简单的对象建模——家长与子女:a Parent has a Child, a Child knowshis/her Parent。在Java 里边很好写,不用担心内存泄漏,也不用担心空悬指针,只要正确初始化myChild 和myParent,那么Java 程序员就不用担心出现访问错误。一个handle 是否有效,只需要判断其是否non null。
public class Parent
{
private Child myChild;
}
public class Child
{
private Parent myParent;
}
在C++ 里边就要为资源管理费一番脑筋。如果使用原始指针作为成员,Child和Parent由谁释放?那么如何保证指针的有效性?如何防止出现空悬指针?这些问题是C++面向对象编程麻烦的问题,现在可以借助smart pointer把对象语义(pointer)转变为值(value)语义,shared_ptr轻松解决生命周期的问题,不必担心空悬指针。但是这个模型存在循环引用的问题,注意其中一个指针应该为weak_ptr。
原始指针的做法,容易出错
#include <iostream> #include <memory> class Child; class Parent; class Parent { private: Child* myChild; public: void setChild(Child* ch) { this->myChild = ch; } void doSomething() { if (this->myChild) { } } ~Parent() { delete myChild; } }; class Child { private: Parent* myParent; public: void setPartent(Parent* p) { this->myParent = p; } void doSomething() { if (this->myParent) { } } ~Child() { delete myParent; } }; int main() { { Parent* p = new Parent; Child* c = new Child; p->setChild(c); c->setPartent(p); delete c; //only delete one } return 0; }
循环引用内存泄露的问题
#include <iostream> #include <memory> class Child; class Parent; class Parent { private: std::shared_ptr<Child> ChildPtr; public: void setChild(std::shared_ptr<Child> child) { this->ChildPtr = child; } void doSomething() { if (this->ChildPtr.use_count()) { } } ~Parent() { } }; class Child { private: std::shared_ptr<Parent> ParentPtr; public: void setPartent(std::shared_ptr<Parent> parent) { this->ParentPtr = parent; } void doSomething() { if (this->ParentPtr.use_count()) { } } ~Child() { } }; int main() { std::weak_ptr<Parent> wpp; std::weak_ptr<Child> wpc; { std::shared_ptr<Parent> p(new Parent); std::shared_ptr<Child> c(new Child); p->setChild(c); c->setPartent(p); wpp = p; wpc = c; std::cout << p.use_count() << std::endl; // 2引用计数 std::cout << c.use_count() << std::endl; // 2 } std::cout << wpp.use_count() << std::endl; // 1 std::cout << wpc.use_count() << std::endl; // 1 return 0; }
正确的做法
#include <iostream> #include <memory> class Child; class Parent; class Parent { private: //std::shared_ptr<Child> ChildPtr; std::weak_ptr<Child> ChildPtr; public: void setChild(std::shared_ptr<Child> child) { this->ChildPtr = child; } void doSomething() { //new shared_ptr if (this->ChildPtr.lock()) { } } ~Parent() { } }; class Child { private: std::shared_ptr<Parent> ParentPtr; public: void setPartent(std::shared_ptr<Parent> parent) { this->ParentPtr = parent; } void doSomething() { if (this->ParentPtr.use_count()) { } } ~Child() { } }; int main() { std::weak_ptr<Parent> wpp; std::weak_ptr<Child> wpc; { std::shared_ptr<Parent> p(new Parent); std::shared_ptr<Child> c(new Child); p->setChild(c); c->setPartent(p); wpp = p; wpc = c; std::cout << p.use_count() << std::endl; // 2 std::cout << c.use_count() << std::endl; // 1 } std::cout << wpp.use_count() << std::endl; // 0 std::cout << wpc.use_count() << std::endl; // 0 return 0; }