C++11中unique_ptr的使用
在c++中,动态内存的管理是通过一对运算符来完成的:new,在动态内存中为对象分配空间并返回一个指向该对象的指针,可以选择对对象进行初始化;delete,接受一个动态对象的指针,销毁该对象,并释放与之关联的内存。
动态内存的使用很容易出问题,因为确保在正确的时间释放内存是极其困难的。有时会忘记释放内存,在这种情况下会产生内存泄露;有时在尚有指针引用内存的情况下就释放了它,在这种情况下就会产生引用非法内存的指针。
为了更容易(同时也更安全)地使用动态内存,c++11标准库提供了两种智能指针(smart pointer)类型来管理动态对象。智能指针的行为类似常规指针,重要的区别是它负责自动释放所指的对象。c++11标准库提供的这两种智能指针的区别在于管理底层指针的方式:shared_ptr允许多个指针指向同一个对象;unique_ptr则"独占"所指向的对象。c++11标准库还定义了一个名为weak_ptr的辅助类,它是一种弱引用,指向shared_ptr所管理的对象。这三种类型都定义在memory头文件中。智能指针是模板类而不是指针。类似vector,智能指针也是模板,当创建一个智能指针时,必须提供额外的信息即指针可以指向的类型。默认初始化的智能指针中保存着一个空指针。智能指针的使用方式与普通指针类似。解引用一个智能指针返回它指向的对象。如果在一个条件判断中使用智能指针,效果就是检测它是否为空。
in c++, a smart pointer is implemented as a template class that mimics, by means of operator overloading, the behaviors of a traditional (raw) pointer, (e.g. dereferencing, assignment) while providing additional memory management features.
std::unique_ptr is a smart pointer that retains sole ownership of an object through a pointer and destroys that object when the unique_ptr goes out of scope. no two unique_ptr instances can manage the same object.
the object is destroyed and its memory deallocated when either of the following happens: (1)、the managing unique_ptr object is destroyed; (2)、the managing unique_ptr object is assigned another pointer via operator= or reset().
a unique_ptr may alternatively own no object, in which case it is called empty.
only non-const unique_ptr can transfer the ownership of the managed object to another unique_ptr. the lifetime of an object managed by const std::unique_ptr is limited to the scope in which the pointer was created.
std::unique_ptr may be constructed for an incomplete type t. conversely, std::shared_ptr can't be constructed from a raw pointer to incomplete type, but can be destroyed where t is incomplete.
a unique_ptr does not share its pointer. it cannot be copied to another unique_ptr, passed by value to a function, or used in any standard template library (stl) algorithm that requires copies to be made. a unique_ptr can only be moved. this means that the ownership of the memory resource is transferred to another unique_ptr and the original unique_ptr no longer owns it.
when using unique_ptr, there can be at most one unique_ptr pointing at any one resource.when that unique_ptr is destroyed, the resource is automatically reclaimed.because there can only be one unique_ptr to any resource, any attempt to make a copy of a unique_ptr will cause a compile-time error. however, unique_ptr can be moved using the new move semantics.
shared_ptr, allows for multiple pointers to point at a given resource. when the very last shared_ptr to a resource is destroyed, the resource will be deallocated. shared_ptr uses reference counting to track how many pointers refer to a resource, so you need to be careful not to introduce any reference cycles.
a unique_ptr is a container for a raw pointer, which the unique_ptr is said to own. a unique_ptr explicitly prevents copying of its contained pointer (as would happen with normal assignment), but the std::move function can be used to transfer ownership of the contained pointer to another unique_ptr. a unique_ptr cannot be copied because its copy constructor and assignment operators are explicitly deleted.
everything you can do with auto_ptr, unique_ptr will do as well.
there are two kinds of unique_ptr, one for scalars (i.e. non-arrays) and one for arrays:
(1)、unique_ptr
(2)、unique_ptr
std::unique_ptr是c++11标准中用来取代std::auto_ptr的指针容器(在c++11中,auto_ptr被废弃)。它不能与其它unique_ptr类型的指针对象共享所指对象的内存。这种”所有权”仅能够通过标准库的move函数来转移。unique_ptr是一个删除了拷贝构造函数、保留了移动构造函数的指针封装类型。
一个unique_ptr"拥有"它所指向的对象。与shared_ptr不同,某个时刻只能有一个unique_ptr指向一个给定对象。当unique_ptr被销毁时,它所指向的对象也被销毁。与shared_ptr不同,在c++11中,没有类似make_shared的标准库函数返回一个unique_ptr。当定义一个unique_ptr时,需要将其绑定到一个new返回的指针上。类似shared_ptr,初始化unique_ptr必须采用直接初始化形式。由于一个unique_ptr拥有它指向的对象,因此unique_ptr不支持普通的拷贝或赋值操作。虽然不能拷贝或赋值unique_ptr,但可以通过调用release或reset将指针的所有权从一个(非const)unique_ptr转移给另一个unique。
调用release会切断unique_ptr和它原来管理的对象间的联系。release返回的指针通过被用来初始化另一个智能指针或给另一个智能指针赋值。如果不用另一个智能指针来保存release返回的指针,程序就要负责资源的释放。
不能拷贝unique_ptr的规则有一个例外:我们可以拷贝或赋值一个将要被销毁的unique_ptr,最常见的例子是从函数返回一个unique_ptr。
类似shared_ptr,unique_ptr默认情况下用delete释放它指向的对象。与shared_ptr一样,可以重载一个unique_ptr中默认的删除器。但是,unique_ptr管理删除器的方式与shared_ptr不同。
下图列出了unique_ptr支持的操作(来源于c++ primer fifth edition 中文版):
下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:
#include "unique_ptr.hpp" #include #include #include #include #include #include #include /////////////////////////////////////////////////////// // reference: https://en.cppreference.com/w/cpp/memory/unique_ptr struct foo { foo() { std::cout << "foo::foo\n"; } ~foo() { std::cout << "foo::~foo\n"; } void bar() { std::cout << "foo::bar\n"; } }; void f(const foo &) { std::cout << "f(const foo&)\n"; } int test_unique_ptr1() { std::unique_ptr p1(new foo); // p1 owns foo if (p1) p1->bar(); { std::unique_ptr p2(std::move(p1)); // now p2 owns foo f(*p2); p1 = std::move(p2); // ownership returns to p1 std::cout << "destroying p2...\n"; } if (p1) p1->bar(); // foo instance is destroyed when p1 goes out of scope return 0; } ////////////////////////////////////////////////////// // reference: https://www.cplusplus.com/reference/memory/unique_ptr/unique_ptr/ int test_unique_ptr2() { std::default_delete d; std::unique_ptr u1; std::unique_ptr u2(nullptr); std::unique_ptr u3(new int); std::unique_ptr u4(new int, d); std::unique_ptr u5(new int, std::default_delete()); std::unique_ptr u6(std::move(u5)); std::unique_ptr u7(std::move(u6)); std::unique_ptr u8(std::auto_ptr(new int)); std::cout << "u1: " << (u1 ? "not null" : "null") << '\n'; std::cout << "u2: " << (u2 ? "not null" : "null") << '\n'; std::cout << "u3: " << (u3 ? "not null" : "null") << '\n'; std::cout << "u4: " << (u4 ? "not null" : "null") << '\n'; std::cout << "u5: " << (u5 ? "not null" : "null") << '\n'; std::cout << "u6: " << (u6 ? "not null" : "null") << '\n'; std::cout << "u7: " << (u7 ? "not null" : "null") << '\n'; std::cout << "u8: " << (u8 ? "not null" : "null") << '\n'; return 0; } ////////////////////////////////////////////////////// // reference: https://eli.thegreenplace.net/2012/06/20/c11-using-unique_ptr-with-standard-library-containers struct foo_0 { foo_0() { std::cerr << "foo_0 [" << this << "] constructed\n"; } virtual ~foo_0() { std::cerr << "foo_0 [" << this << "] destructed\n"; } }; void sink(std::unique_ptr p) { std::cerr << "sink owns foo_0 [" << p.get() << "]\n"; } std::unique_ptr source() { std::cerr << "creating foo_0 in source\n"; return std::unique_ptr(new foo_0); } int test_unique_ptr3() { std::cerr << "calling source\n"; std::unique_ptr pmain = source(); // can also be written as // auto pmain = source(); std::cerr << "now pmain owns foo [" << pmain.get() << "]\n"; std::cerr << "passing it to sink\n"; // sink(pmain); // error! can't copy unique_ptr sink(move(pmain)); // ok: can move it! std::cerr << "main done\n"; return 0; } //////////////////////////////////////////////////// // reference: https://www.codeguru.com/cpp/article.php/c17775/the-smart-pointer-that-makes-your-c-applications-safer--stduniqueptr.htm void func(int*) { } int test_unique_ptr4() { // default construction std::unique_ptr up; //creates an empty object // initialize with an argument std::unique_ptr uptr(new int(3)); double *pd = new double; std::unique_ptr uptr2(pd); // overloaded * and -> *uptr2 = 23.5; std::unique_ptr ups(new std::string("hello")); int len = ups->size(); // reset() releases the owned resource and optionally acquires a new resource: uptr2.reset(new double); //delete pd and acquire a new pointer uptr2.reset(); //delete the pointer acquired by the previous reset() call // if you need to access the owned pointer directly use get() func(uptr.get()); // unique_ptr has implicit conversion to bool. // this lets you use unique_ptr object in boolean expressions such as this: if (ups) //implicit conversion to bool std::cout << *ups << std::endl; else std::cout << "an empty smart pointer" << std::endl; // array support: unique_ptr can store arrays as well. // a unique_ptr that owns an array defines an overloaded operator []. // obviously, the * and -> operators are not available. // additionally, the default deleter calls delete[] instead of delete: std::unique_ptr arrup(new int[5]); arrup[0] = 5; // std::cout << *arrup << std::endl; //error, operator * not defined // compatibility with containers and algorithms // you can safely store unique_ptr in standard library containers and let algorithms manipulate sequences of unique_ptr objects. std::vector> vi; vi.push_back(std::unique_ptr(new int(0))); // populate vector vi.push_back(std::unique_ptr(new int(3))); vi.push_back(std::unique_ptr(new int(2))); std::sort(vi.begin(), vi.end()); // result: {0, 2, 3} return 0; }
github:https://github.com/fengbingchun/messy_test
下一篇: c++11实现一个半同步半异步线程池