C++ 智能指针 shared_ptr 详解与示例_码农小明的博客-CSDN博客_shared_ptr
shared_ptr 是c++11的智能类,可以在任何地方都不使用的时候自动删除和相关指针,从而彻底消除内存泄漏和指针悬空的问题。
她遵循共享所有权,即不同的shared_ptr 对象可以与相同的指针想关联
每个shared_ptr对象在内部纸箱两个内存位置:
1、指向对象的指针
2、用于控制引用计数数据的指针
- std::shared_ptr<int> p1(new int()); // 有两个内存,1是存放int 的地址,2 是用来引用计数的内存地址(在哪里引用了)
- p1.use_count(); // 这个指针别引用了多长次
std::shared_ptr<int> ptr1 = std::make_shared<int>();
因为带有参数的 shared_ptr 构造函数是 explicit 类型的,所以不能像这样
std::shared_ptr
隐式调用它构造函数。创建新的shared_ptr对象的最佳方法是使用std :: make_shared:p1 = new int();
这里可以说一下explicit 的所用:
explicit关键字用来修饰类的构造函数,被修饰的构造函数的类,不能发生相应的隐式类型转换,只能以显示的方式进行类型转换。
使用reset() 函数,但是这里有两种方式reset()和reset(params);
不带参数的reset()
- p1.reset(); // 引用的计数会减一,如果计数为0 ,那么这个指针就会删除
-
带参数的reset():
p1.reset(new int(34)); // 将内部指向新的指针,因此其作用计算将变成1
使用nullptr重置:
p1 = nullptr;
- if 1
-
- int main()
- {
-
- // std::shared_ptr
p1(new int()); // 有两个内存,1是存放int 的地址,2 是用来引用计数的内存地址(在哪里引用了) - //p1.use_count(); // 这个指针别引用了多长次
-
-
- // 创建一个空对象
- std::shared_ptr<int> p1 = std::make_shared<int>();
- std::cout << "p1 空 的引用次数 " << p1.use_count() << std::endl;
- *p1 = 3334;
- std::cout << " p1= " <<*p1 <
//输出3334 - std::cout << " p1的内存地址 " << p1 << std::endl; //
-
- // 打印引用个数:1
- std::cout << "p1 的引用次数 " << p1.use_count() << std::endl;
- // 创建第二个对象p2 使其指向同一个指针
- std::shared_ptr<int> p2(p1);
- std::cout << " p2的内存地址 " << p2 << std::endl; //
- // 下面两个输出都是:2
- std::cout << "p2 引用次数 = " << p2.use_count() << std::endl;
- std::cout << "p1 引用次数 = " << p1.use_count() << std::endl;
-
-
- // 比较智能指针,p1 等于 p2 ,由于p1 p2 的地址是一样的,所以p1==p2
- if (p1 == p2)
- {
- std::cout << "p1 and p2 are pointing to same pointer\n";
- }
- std::cout << "Reset p1 " << std::endl;
-
- // 无参数调用reset,无关联指针,引用个数为0
- p1.reset();
- std::cout << "p1 Reference Count = " << p1.use_count() << std::endl;
-
- // 带参数调用reset,引用个数为1
- p1.reset(new int(11));
- std::cout << "p1 Reference Count = " << p1.use_count() << std::endl;
-
- // 把对象重置为NULL,引用计数为0
- p1 = nullptr;
- std::cout << "p1 Reference Count = " << p1.use_count() << std::endl;
- if (!p1) {
- std::cout << "p1 is NULL" << std::endl; // 输出
- }
-
-
- return 0;
-
- }
- # endif
4、自定义删除器 Deleter
delete Pointer;
当 shared_ptr 对象指向数组
- // 需要添加自定义删除器的使用方式
- std::shared_ptr<int> p3(new int[12]); // 仅用于演示自定义删除器
-
- // 指向数组的智能指针可以使用这种形式
- std::shared_ptr<int[]> p3(new int[12]); // 正确使用方式
给shared_ptr添加自定义删除器
- #if 1 // share_ptr 指针的使用
-
- struct A
- {
- A()
- {
- std::cout << " A \n";
- }
- ~A()
- {
- std::cout << " ~A \n";
- }
- };
-
- void deleter(A* a)
- {
- std::cout << "A Deleter\n";
- delete[] a;
- }
-
-
- int main()
- {
- std::shared_ptrp4(new A[3], deleter);
-
-
- std::cout << "--------------------------------------------------------\n";
-
- // std::shared_ptr
p1(new int()); // 有两个内存,1是存放int 的地址,2 是用来引用计数的内存地址(在哪里引用了) - //p1.use_count(); // 这个指针别引用了多长次
-
-
- // 创建一个空对象
- std::shared_ptr<int> p1 = std::make_shared<int>();
- std::cout << "p1 空 的引用次数 " << p1.use_count() << std::endl;
- *p1 = 3334;
- std::cout << " p1= " <<*p1 <
//输出3334 - std::cout << " p1的内存地址 " << p1 << std::endl; //
-
- // 打印引用个数:1
- std::cout << "p1 的引用次数 " << p1.use_count() << std::endl;
- // 创建第二个对象p2 使其指向同一个指针
- std::shared_ptr<int> p2(p1);
- std::cout << " p2的内存地址 " << p2 << std::endl; //
- // 下面两个输出都是:2
- std::cout << "p2 引用次数 = " << p2.use_count() << std::endl;
- std::cout << "p1 引用次数 = " << p1.use_count() << std::endl;
-
-
- // 比较智能指针,p1 等于 p2 ,由于p1 p2 的地址是一样的,所以p1==p2
- if (p1 == p2)
- {
- std::cout << "p1 and p2 are pointing to same pointer\n";
- }
- std::cout << "Reset p1 " << std::endl;
-
- // 无参数调用reset,无关联指针,引用个数为0
- p1.reset();
- std::cout << "p1 Reference Count = " << p1.use_count() << std::endl;
-
- // 带参数调用reset,引用个数为1
- p1.reset(new int(11));
- std::cout << "p1 Reference Count = " << p1.use_count() << std::endl;
-
- // 把对象重置为NULL,引用计数为0
- p1 = nullptr;
- std::cout << "p1 Reference Count = " << p1.use_count() << std::endl;
- if (!p1) {
- std::cout << "p1 is NULL" << std::endl; // 输出
- }
-
-
- return 0;
-
- }
- # endif
缺少 ++, – – 和 [] 运算符
与普通指针相比,shared_ptr仅提供->
、*
和==
运算符,没有+
、-
、++
、--
、[]
等运算符。
当我们创建 shared_ptr 对象而不分配任何值时,它就是空的;普通指针不分配空间的时候相当于一个野指针,指向垃圾空间,且无法判断指向的是否是有用数据。
不要使用同一个原始指针构造 shared_ptr
创建多个 shared_ptr 的正常方法是使用一个已存在的shared_ptr 进行创建,而不是使用同一个原始指针进行创建。
示例:
- int *num = new int(23);
- std::shared_ptr<int> p1(num);
-
- std::shared_ptr<int> p2(p1); // 正确使用方法
- std::shared_ptr<int> p3(num); // 不推荐
-
- std::cout << "p1 Reference = " << p1.use_count() << std::endl; // 输出 2
- std::cout << "p2 Reference = " << p2.use_count() << std::endl; // 输出 2
- std::cout << "p3 Reference = " << p3.use_count() << std::endl; // 输出 1
假如使用原始指针num创建了p1,又同样方法创建了p3,当p1超出作用域时会调用delete释放num内存,此时num成了悬空指针,当p3超出作用域再次delete的时候就可能会出错。
不要用栈中的指针构造 shared_ptr 对象
shared_ptr 默认的构造函数中使用的是delete来删除关联的指针,所以构造的时候也必须使用new出来的堆空间的指针。
示例:
- int main()
- {
- int x = 12;
- std::shared_ptr<int> ptr(&x);
- return 0;
- }
当 shared_ptr 对象超出作用域调用析构函数delete 指针&x时会出错。
为了避免以上两种情形,建议使用make_shared()<>创建 shared_ptr 对象,而不是使用默认构造函数创建。
- std::shared_ptr<int> ptr_1 = make_shared<int>();
- std::shared_ptr<int> ptr_2 (ptr_1);
另外不建议使用get()函数获取 shared_ptr 关联的原始指针,因为如果在 shared_ptr 析构之前手动调用了delete函数,同样会导致类似的错误。
Leetcode——C++突击面试_leetcode c++ 面试突破-CSDN博客
共享指针(shared_ptr):资源可以被多个指针共享,使用计数机制表明资源被几个指针共享。通过 use_count() 查看资源的所有者的个数,可以通过 unique_ptr、weak_ptr 来构造,调用 release() 释放资源的所有权,计数减一,当计数减为 0 时,会自动释放内存空间,从而避免了内存泄漏。
独占指针(unique_ptr):独享所有权的智能指针,资源只能被一个指针占有,该指针不能拷贝构造和赋值。但可以进行移动构造和移动赋值构造(调用move() 函数),即一个 unique_ptr 对象赋值给另一个 unique_ptr 对象,可以通过该方法进行赋值。
弱指针(weak_ptr):指向 shared_ptr 指向的对象,能够解决由shared_ptr带来的循环引用问题。
使用std::move() 函数将一个对象 全权转移给另一个对象
- std::unique_ptr
ptr1Child(new Child()) ; - std::unique_ptr
ptrChild2 = std::move(ptr1Child); // 将unique_ptr 指针全部给ptrChild2
循环引用
如上面说,我们使用智能指针的时候 ,按理来说我们的程序在结束的时候,share_ptr 的count=0
但是我们相互定义使用
- #include
- #include
-
- using namespace std;
-
- class Child;
- class Parent;
-
- class Parent {
- private:
- shared_ptr
ChildPtr; - public:
- void setChild(shared_ptr
child) { - this->ChildPtr = child;
- }
-
- void doSomething() {
- if (this->ChildPtr.use_count()) {
-
- }
- }
-
- ~Parent() {
- }
- };
-
- class Child {
- private:
- shared_ptr
ParentPtr; - public:
- void setPartent(shared_ptr
parent) { - this->ParentPtr = parent;
- }
- void doSomething() {
- if (this->ParentPtr.use_count()) {
-
- }
- }
- ~Child() {
- }
- };
-
- int main() {
- weak_ptr
wpp; - weak_ptr
wpc; - {
- shared_ptr
p(new Parent) ; - shared_ptr
c(new Child) ; - p->setChild(c);
- c->setPartent(p);
- wpp = p;
- wpc = c;
- cout << p.use_count() << endl; // 2
- cout << c.use_count() << endl; // 2
- }
- cout << wpp.use_count() << endl; // 1
- cout << wpc.use_count() << endl; // 1
- return 0;
- }
循环引用的解决方法: weak_ptr
循环引用:该被调用的析构函数没有被调用,从而出现了内存泄漏。
- #include
- #include
-
- using namespace std;
-
- class Child;
- class Parent;
-
- class Parent {
- private:
- //shared_ptr
ChildPtr; - weak_ptr
ChildPtr; - public:
- void setChild(shared_ptr
child) { - this->ChildPtr = child;
- }
-
- void doSomething() {
- //new shared_ptr
- if (this->ChildPtr.lock()) {
-
- }
- }
-
- ~Parent() {
- }
- };
-
- class Child {
- private:
- shared_ptr
ParentPtr; - public:
- void setPartent(shared_ptr
parent) { - this->ParentPtr = parent;
- }
- void doSomething() {
- if (this->ParentPtr.use_count()) {
-
- }
- }
- ~Child() {
- }
- };
-
- int main() {
- weak_ptr
wpp; - weak_ptr
wpc; - {
- shared_ptr
p(new Parent) ; - shared_ptr
c(new Child) ; - p->setChild(c);
- c->setPartent(p);
- wpp = p;
- wpc = c;
- cout << p.use_count() << endl; // 2
- cout << c.use_count() << endl; // 1
- }
- cout << wpp.use_count() << endl; // 0
- cout << wpc.use_count() << endl; // 0
- return 0;
- }