智能指针 shared_ptr 使用

上一篇智能指针是啥玩意,介绍了什么是智能指针。

这一篇简单说说如何使用智能指针。

一,智能指针分3类:今天只唠唠shared_ptr

  • shared_ptr
  • unique_ptr
  • weak_ptr

二,下表是shared_ptr和unique_ptr都支持的操作

操作 功能描述
shared_ptr<T> sp 空智能指针,可以指向类型为T的对象
unique_ptr<T> up 空智能指针,可以指向类型为T的对象
p 将p用作一个条件判断,如果p指向一个对象,则为true
*p 解引用p,获得它指向的对象
p->mem 等价于(*p).mem,访问p所指对象的mem成员
p.get() 返回p中保存的指针。如果指向的对象已经被释放,就是一个危险的指针
swap(p, q)或者p.swap(q) 交换p和q中的指针

上面操作的验证代码

#include <memory>
#include <iostream>
#include <vector> using namespace std; class Test{
public:
Test(int d = 0):data(d){cout << "cr:" << data << endl;}
~Test(){cout << "fr:" << data << endl;}
void fun(){
cout << "Test func(" << data << ")" << endl;
}
private:
int data;
};
int main(){
//shared_ptr<Test> sp = make_shared<Test>();
Test* pt = new Test();
shared_ptr<Test> sp(pt);
if(sp){
cout << "sp指向了对象" << endl;
}
(*sp).fun();
shared_ptr<int> isp;
if(!isp){
cout << "isp没有指向对象" << endl;
}
Test* tmp1 = sp.get();
auto sp1 = make_shared<Test>(10);
Test* tmp2 = sp1.get();
swap(sp, sp1);
tmp1->fun();//0
tmp2->fun();//10
//sp和sp1所指向的对象被交换了
sp.get()->fun();//10
sp1.get()->fun();//0 }

三,下表是shared_ptr独有的操作

操作 功能描述
make_shared<T>(args) 返回shared_ptr,指向一个动态分配的类型为T的对象。使用args初始化此对象。
shared_ptr p(q) p是q的拷贝;递增q中的计数器。q中的指针必须能转化成T*。
p = q p和q都是shared_ptr,所保存的指针必须能相互转换。递减p的引用计数;递增q的引用计数;如果p的引用计数变为0,则释放p管理的对象的内存。
p.unique() 如果p.use_count()为1,则返回true;否则返回false
p.use_count() 返回与p共享对象的智能指针的数量;性能很低,用于调试。

上面操作的验证代码

  shared_ptr<Test> tsp = make_shared<Test>(11);
cout << tsp.use_count() << endl;//1
//tsp1和tsp指向相同的对象,这个对象的计数器加1
shared_ptr<Test> tsp1(tsp);
cout << tsp.use_count() << endl;//2
//用tsp1改变了对象的data的值,所以用tsp再访问这个对象,发现对象被改变了
tsp1->setData(111);
tsp->fun();//111 shared_ptr<Test> q(new Test(20));
cout << q.use_count() << endl;//1
cout << tsp.use_count() << endl;//2
//如果q不是智能指针,q指向的Test(20)这块内存就泄露了
//q是智能指针,所以自动释放了Test(20)这块内存
q = tsp;
cout << q.use_count() << endl;//3
cout << tsp.use_count() << endl;//3
if(!q.unique()){
cout << "不是只有一个智能指针指向了某个对象" << endl;
}

四,智能指针作为函数的返回值

shared_ptr<Test> hun(int d){
return make_shared<Test>(d);
}
void use_hun1(int d){
shared_ptr<Test> p = hun(d);
p->fun();
}//p离开作用域后,它指向的内存会被自动释放
shared_ptr<Test> use_hun2(int d){
shared_ptr<Test> p = hun(d);//计数器为1
return p;//返回p时,计数器递增,为2
}//离开作用域后,计数器递减,为1,因为不为0,所以不会释放

一到四的小例子:

include <memory>
#include <iostream>
#include <vector> using namespace std; class Test{
public:
Test(int d = 0):data(d){cout << "cr:" << data << endl;}
~Test(){cout << "fr:" << data << endl;}
void fun(){
cout << "Test func(" << data << ")" << endl;
}
void setData(int d){
data = d;
}
private:
int data;
}; //test3 智能指针作为函数的返回值
shared_ptr<Test> hun(int d){
return make_shared<Test>(d);
}
void use_hun1(int d){
shared_ptr<Test> p = hun(d);
p->fun();
}//p离开作用域后,它指向的内存会被自动释放
shared_ptr<Test> use_hun2(int d){
shared_ptr<Test> p = hun(d);//计数器为1
return p;//返回p时,计数器递增,为2
}//离开作用域后,计数器递减,为1,因为不为0,所以不会释放
int main(){
//test1 shared_ptr和unique_ptr都支持的操作
/*
//shared_ptr<Test> sp = make_shared<Test>();
Test* pt = new Test();
shared_ptr<Test> sp(pt);
if(sp){
cout << "sp指向了对象" << endl;
}
(*sp).fun();
shared_ptr<int> isp;
if(!isp){
cout << "isp没有指向对象" << endl;
}
Test* tmp1 = sp.get();
auto sp1 = make_shared<Test>(10);
Test* tmp2 = sp1.get();
swap(sp, sp1);
tmp1->fun();
tmp2->fun();
sp.get()->fun();
sp1.get()->fun();
*/ //test2 shared_ptr独有的操作
/*
shared_ptr<Test> tsp = make_shared<Test>(11);
cout << tsp.use_count() << endl;//1
//tsp1和tsp指向相同的对象,这个对象的计数器加1
shared_ptr<Test> tsp1(tsp);
cout << tsp.use_count() << endl;//2
//用tsp1改变了对象的data的值,所以用tsp再访问这个对象,发现对象被改变了
tsp1->setData(111);
tsp->fun();//111 shared_ptr<Test> q(new Test(20));
cout << q.use_count() << endl;//1
cout << tsp.use_count() << endl;//2
//如果q不是智能指针,q指向的Test(20)这块内存就泄露了
//q是智能指针,所以自动释放了Test(20)这块内存
q = tsp;
cout << q.use_count() << endl;//3
cout << tsp.use_count() << endl;//3
if(!q.unique()){
cout << "不是只有一个智能指针指向了某个对象" << endl;
}
*/ //test3 智能指针作为函数的返回值
/*
auto ap = use_hun2(22);
ap->fun();
use_hun1(33);
*/ }

github完整代码

五,智能指针的注意事项

把shared_ptr放入容器中时,之后不再需要全部元素,只使用其中一部分的话,要用erase删除那些不再需要使用的shared_ptr。如果不erase那些不再需要使用的shared_ptr,shared_ptr就不会释放它指向的内存。

六,智能指针的小例子,让多个对象共享相同的状态。

  • 有个类shared_vector,里面有个shared_ptr,指向了一个vector,类shared_vector的对象a2拷贝a1时,实现a1和a2共享vector。
  • 类un_shared_vector没有使用shared_ptr,所以没有共享vector。
include <iostream>
#include <memory>
#include <vector>
#include <string> using namespace std; class shared_vector{
public:
typedef vector<string>::size_type size_type;
shared_vector():data(make_shared<vector<string>>()){}
shared_vector(initializer_list<string> il):
data(make_shared<vector<string>>(il)){}
size_type size()const{return data->size();}
bool empty()const{return data->empty();}
//尾部插入,删除元素
void push_back(const string& s){data->push_back(s);}
void pop_back(){data->pop_back();}
//访问元素
string& front(){return data->front();}
string& back(){return data->back();} private:
shared_ptr<vector<string>> data;
}; class un_shared_vector{
public:
typedef vector<string>::size_type size_type;
un_shared_vector():data(vector<string>()){}
un_shared_vector(initializer_list<string> il):data(il){}
size_type size()const{return data.size();}
bool empty()const{return data.empty();}
//尾部插入,删除元素
void push_back(const string& s){data.push_back(s);}
void pop_back(){data.pop_back();}
//访问元素
string& front(){return data.front();}
string& back(){return data.back();} private:
vector<string> data;
}; int main(){
shared_vector sv{"aa","bb"};
shared_vector sv1(sv);
//因为sv和sv1共享同一个vector,
//所以通过sv改变vector后,通过sv1也发现了相同的改变
sv.push_back("cc");
cout << sv1.back() << endl; un_shared_vector usv{"11","22"};
un_shared_vector usv1(usv);
//因为usv和usv1不共享同一个vector,
//所以通过usv改变vector后,usv1里面的vector没有跟着变化
usv.push_back("33");
cout << usv1.back() << endl;
cout << usv.back() << endl;
}

github完整代码

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

c/c++ 智能指针 shared_ptr 使用的更多相关文章

  1. c/c++ 智能指针 shared_ptr 和 new结合使用

    智能指针 shared_ptr 和 new结合使用 用make_shared函数初始化shared_ptr是最推荐的,但有的时候还是需要用new关键字来初始化shared_ptr. 一,先来个表格,唠 ...

  2. C++智能指针shared_ptr

    shared_ptr 这里有一个你在标准库中找不到的—引用数智能指针.大部分人都应当有过使用智能指针的经历,并且已经有很多关于引用数的文章.最重要的一个细节是引用数是如何被执行的—插入,意思是说你将引 ...

  3. STL源码剖析-智能指针shared_ptr源码

    目录一. 引言二. 代码实现 2.1 模拟实现shared_ptr2.2 测试用例三. 潜在问题分析 你可能还需要了解模拟实现C++标准库中的auto_ptr一. 引言与auto_ptr大同小异,sh ...

  4. 智能指针shared_ptr的用法

    为了解决C++内存泄漏的问题,C++11引入了智能指针(Smart Pointer). 智能指针的原理是,接受一个申请好的内存地址,构造一个保存在栈上的智能指针对象,当程序退出栈的作用域范围后,由于栈 ...

  5. 智能指针 shared_ptr 解析

    近期正在进行<Effective C++>的第二遍阅读,书里面多个条款涉及到了shared_ptr智能指针,介绍的太分散,学习起来麻烦.写篇blog整理一下. LinJM   @HQU s ...

  6. 智能指针shared_ptr

    // 智能指针会自动释放所指向的对象. // shared_ptr的应用场景是:程序需要在多个对象间共享数据 /* 先从应用场景入手吧,说矿工A发现了一个金矿. * 然后矿工A喊来了矿工B,一起开采, ...

  7. 智能指针shared_ptr新特性shared_from_this及weak_ptr

    enable_shared_from_this是一个模板类,定义于头文件<memory>,其原型为: template< class T > class enable_shar ...

  8. C++ 智能指针 shared_ptr

    今天晚上去旁听了C++高级编程的课,其中提到智能指针.第一反映还以为是auto_ptr呢,一听才知道是share_ptr这个.哦,原来是C++11特性.大致的原因是auto_ptr有一点缺陷,而sha ...

  9. 标准库中的智能指针shared_ptr

    智能指针的出现是为了能够更加方便的解决动态内存的管理问题.注:曾经记得有本书上说可以通过vector来实现动态分配的内存的自动管理,但是经过试验,在gcc4.8.5下是不行的.这个是容易理解的,vec ...

随机推荐

  1. Tomcat8源码笔记(八)明白Tomcat怎么部署webapps下项目

    以前没想过这么个问题:Tomcat怎么处理webapps下项目,并且我访问浏览器ip: port/项目名/请求路径,以SSM为例,Tomcat怎么就能将请求找到项目呢,项目还是个文件夹类型的? Tom ...

  2. 【ASP.NET MVC系列】浅谈ASP.NET 页面之间传值的几种方式

    ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...

  3. Tomcat(一):背景知识和安装tomcat

    Tomcat系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html 1. 基础背景知识 1.1 java和jdk概念 无论是何种程序,要能在计算机 ...

  4. ZooKeeper系列(5):ZooKeeper的日志和快照

    ZooKeeper系列文章:https://www.cnblogs.com/f-ck-need-u/p/7576137.html#zk ZooKeeper有两种日志.一种快照.日志分为事务日志和Zoo ...

  5. QT 设置有效绘图区域

    void QPainter::setClipRect(int x, int y, int width, int height, Qt::ClipOperation operation = Qt::Re ...

  6. μC/OS-II 创建一个任务的流程

    1. 声明任务的优先级.任务堆栈的大小及任务函数 #define LED_TASK_PRIO 6 #define LED_STK_SIZE 64 void led_task(void *pdata); ...

  7. MySQL之实现Oracle中的rank()函数的功能

      假设表格为student, 数据如下:   我们要在MySQL中实现Oracle中的rank()函数功能,即组内排序,具体来说: 就是对student表中按照课程(course)对学生(name) ...

  8. Docker在Linux上运行NetCore系列(四)使用私有Nuget与多个本地包引用运行ASPNetCore

    转发请注明此文章作者与路径,请尊重原著,违者必究. 本篇文章演示了使用Dockerfile在Linux(ubuntu16.04)系统上构建ASPNetCore应用,并且在一个解决方案中存在多个项目之间 ...

  9. C# Redis安装 使用教程

    前言:lz自打工作以来第一次遇到电脑问题需要重装系统,全盘格式化.打击是沉痛的.特别伤.  然后需要重新在本地部署 redis.这是写这篇博客的原因.希望对大家有所帮助,安装资源和引用DLL可以引用 ...

  10. [nodejs] nodejs开发个人博客(六)数据分页

    控制器路由定义 首页路由:http://localhost:8888/ 首页分页路由:http://localhost:8888/index/2 /** * 首页控制器 */ var router=e ...