c++智能指针(2)
追加一个shared_ptr指针
#include <memory> using namespace std; int _tmain(int argc, _TCHAR* argv[])
{
//===========================
// 错误 两个只能指针 管理一个已经分配的内存
//int* p = new int(7);
//shared_ptr<int> p1(p);
//shared_ptr<int> p2(p);
//============================ //================================
// 正确做法
shared_ptr<int> sp1(new int());
shared_ptr<int> sp2(sp1); //
//================================ return ;
}
weak_ptr示例
/*
// 使用shred_ptr的主要原因就是避免关注指针指向的资源
// 只能指针将自动释放与不再需要的对象的相关资源
// 但是某些情况下,这种却不是我们需要的。
// 比如 循环引用.两个对象都引用对方。
// 又或者 分享一个对象 但是不占有该对象
// 这个时候可以使用weak_ptr 其实相对shared_ptr 该指针不增加计数
//
*/ #include <iostream>
#include <string>
#include <vector>
#include <memory> using namespace std; class Person{
public:
string name;
shared_ptr<Person> mother;
shared_ptr<Person> father;
vector<shared_ptr<Person>> kids; Person(const string& n,
shared_ptr<Person> m = nullptr,
shared_ptr<Person> f = nullptr)
:name(n), mother(m), father(f){
} ~Person(){
cout << "delete " << name << endl;
}
}; shared_ptr<Person> initFamily(const string& name)
{
shared_ptr<Person> mom(new Person(name + "'s mom"));
shared_ptr<Person> dad(new Person(name + "'s dad"));
shared_ptr<Person> kid(new Person(name,mom,dad));
mom->kids.push_back(kid);
dad->kids.push_back(kid);
return kid;
} int _tmain(int argc, _TCHAR* argv[])
{
shared_ptr<Person> p = initFamily("nico"); cout << "nico's family exists" << endl;
cout << "- nico is shared " << p.use_count() << " times" << endl;
cout << "- name of 1st kid of nico's mom: "
<< p->mother->kids[]->name << endl; p = initFamily("jim");
cout << "jim's family exists" << endl; return ;
}
运行代码结果如下:
nico's family exists
- nico is shared 3 times
- name of 1st kid of nico's mom: nico
jim's family exists
请按任意键继续. . .
实际上我们并没有看到 指针指向资源的释放 这是因为在class Person 中的vector中 使用了shared_ptr 指针增加了资源的计数 所以没有及时释放
我们应该将vector这个容器改为weak_ptr 同时在使用上 weak_ptr 需要使用lock() 函数将其提升为shared_ptr
/*
// 使用shred_ptr的主要原因就是避免关注指针指向的资源
// 只能指针将自动释放与不再需要的对象的相关资源
// 但是某些情况下,这种却不是我们需要的。
// 比如 循环引用.两个对象都引用对方。
// 又或者 分享一个对象 但是不占有该对象
// 这个时候可以使用weak_ptr 其实相对shared_ptr 该指针不增加计数
//
*/ #include <iostream>
#include <string>
#include <vector>
#include <memory> using namespace std; class Person {
public:
string name;
shared_ptr<Person> mother;
shared_ptr<Person> father;
vector<weak_ptr<Person>> kids; //weakpointer!!!
Person(const string& n,
shared_ptr<Person> m = nullptr,
shared_ptr<Person> f = nullptr)
: name(n), mother(m), father(f) {
}
~Person() {
cout << "delete " << name << endl;
}
}; shared_ptr<Person> initFamily(const string& name)
{
shared_ptr<Person> mom(new Person(name + "'s mom"));
shared_ptr<Person> dad(new Person(name + "'s dad"));
shared_ptr<Person> kid(new Person(name,mom,dad));
mom->kids.push_back(kid);
dad->kids.push_back(kid);
return kid;
} int _tmain(int argc, _TCHAR* argv[])
{
shared_ptr<Person> p = initFamily("nico"); cout << "nico's family exists" << endl;
cout << "- nico is shared " << p.use_count() << " times" << endl;
cout << "- name of 1st kid of nico's mom: "
<< p->mother->kids[].lock()->name << endl; p = initFamily("jim");
cout << "jim's family exists" << endl; return ;
}
运行结果如下
nico's family exists
- nico is shared 1 times
- name of 1st kid of nico's mom: nico
delete nico
delete nico's dad
delete nico's mom
jim's family exists
delete jim
delete jim's dad
delete jim's mom
请按任意键继续. . .
可以看到 资源正确释放
代码 改编自 C++标准库——自学教程与参考手册 英文第二版
c++智能指针(2)的更多相关文章
- enote笔记法使用范例(2)——指针(1)智能指针
要知道什么是智能指针,首先了解什么称为 “资源分配即初始化” what RAII:RAII—Resource Acquisition Is Initialization,即“资源分配即初始化” 在&l ...
- C++11 shared_ptr 智能指针 的使用,避免内存泄露
多线程程序经常会遇到在某个线程A创建了一个对象,这个对象需要在线程B使用, 在没有shared_ptr时,因为线程A,B结束时间不确定,即在A或B线程先释放这个对象都有可能造成另一个线程崩溃, 所以为 ...
- C++智能指针
引用计数技术及智能指针的简单实现 基础对象类 class Point { public: Point(int xVal = 0, int yVal = 0) : x(xVal), y(yVal) { ...
- EC笔记:第三部分:17、使用独立的语句将newed对象放入智能指针
一般的智能指针都是通过一个普通指针来初始化,所以很容易写出以下的代码: #include <iostream> using namespace std; int func1(){ //返回 ...
- 智能指针shared_ptr的用法
为了解决C++内存泄漏的问题,C++11引入了智能指针(Smart Pointer). 智能指针的原理是,接受一个申请好的内存地址,构造一个保存在栈上的智能指针对象,当程序退出栈的作用域范围后,由于栈 ...
- 智能指针unique_ptr的用法
unique_ptr是独占型的智能指针,它不允许其他的智能指针共享其内部的指针,不允许通过赋值将一个unique_ptr赋值给另一个unique_ptr,如下面错误用法: std::unique_pt ...
- 基于C/S架构的3D对战网络游戏C++框架_05搭建系统开发环境与Boost智能指针、内存池初步了解
本系列博客主要是以对战游戏为背景介绍3D对战网络游戏常用的开发技术以及C++高级编程技巧,有了这些知识,就可以开发出中小型游戏项目或3D工业仿真项目. 笔者将分为以下三个部分向大家介绍(每日更新): ...
- C++ 引用计数技术及智能指针的简单实现
一直以来都对智能指针一知半解,看C++Primer中也讲的不够清晰明白(大概是我功力不够吧).最近花了点时间认真看了智能指针,特地来写这篇文章. 1.智能指针是什么 简单来说,智能指针是一个类,它对普 ...
- C++11智能指针读书笔记;
智能指针是一个类对象,而非一个指针对象. 原始指针:通过new建立的*指针 智能指针:通过智能指针关键字(unique_ptr, shared_ptr ,weak_ptr)建立的指针 它的一种通用实现 ...
- 「C++」理解智能指针
维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...
随机推荐
- 老代码:js实现二级城市联动(MVC)
FormViewCity 为mvc控制器传给view的数据,包括一个MyCitys集合字段. <%@ Page Title="" Language="C#" ...
- msf客户端渗透(七):跳板、post模块、自动运行脚本
跳板 假设有这样一个场景,有一个局域网内网网关是1.1.1.1,局域网里的主机1是kali,它经过一个防火墙连接到公网,主机2和主机3在另一个内网网关为2.1.1.1的局域网,由于防火墙做了设置,只有 ...
- weechat 常用指令
添加服务器: /server add freenode irc.freenode.org 设置nick: /set irc.server.freenode.nicks "mynick,myn ...
- 织梦 列表页 list标签 按照自已设置的方式排序
一.可以按照权重排序 降序排序 desc 1.添加的文章默认权重是自动加1,所以只要把想置顶的文章权重设置很高,如10000 2.{dede:list pagesize='12′ orderby='w ...
- serv-U使用
该软件是设置ftp服务器的 可以百度查询ftp服务器安装攻略,如 https://jingyan.baidu.com/article/cb5d6105c00bba005c2fe0ca.html 问题: ...
- laravel5.6上传图片及显示
借鉴大神博客:https://blog.csdn.net/tony_110/article/details/80105099文档:http://laravelacademy.org/post/8965 ...
- 【转】关于easyui的窗口和tab页面不执行js说明
原地址:http://www.jeasyuicn.com/post-49.html 一直以来群里里面很多人反应,在用tab加载界面的时候,界面里面的js不会执行.今天GodSon在此说明一下原因. 不 ...
- Matches Game
Matches Game http://poj.org/problem?id=2234 Time Limit: 1000MS Memory Limit: 65536K Total Submissi ...
- CyclicBarrier簡介
package CyclicBarrier; import java.util.concurrent.CyclicBarrier;import java.util.concurrent.atomic. ...
- C++ map与unordered_map
map与unordered_map对比 map unordered_map 红黑树(非严格二叉平衡搜索树)实现 哈希表实现 有序 无序 -- 查找时间复杂度为O(1),非常快 空间消耗较大 空间消耗较 ...