- 浏览: 715266 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (1042)
- 数据结构 (36)
- UML与设计模式 (42)
- c++ (87)
- rust (36)
- Qt (41)
- boost模板元编程 (43)
- Linux (77)
- 汇编 (4)
- 其它 (2)
- 烹饪 (3)
- unix c / socket (73)
- 软件工程 (4)
- shell (52)
- Python (37)
- c++ primer 5th(c++11) (22)
- 数据库/MySQL (27)
- 数据存储 (4)
- lisp (7)
- git (4)
- Utility (3)
- CDN与DNS (54)
- Http (53)
- php (7)
- nginx/lua/openresty (41)
- redis (11)
- TCP/IP (16)
- 互联网 (6)
- kernel (2)
- go (33)
- 区块链 (43)
- 比特股 (13)
- 以太坊 (23)
- 比特币 (23)
- 密码学 (10)
- EOS (53)
- DAG (1)
- docker (1)
- filecoin (7)
- solidity (64)
- ipfs (8)
- 零知识证明 (1)
- openzeppelin (3)
- java (1)
- defi (7)
最新评论
#include <memory> #include <string> #include <iostream> using namespace std; int main() { shared_ptr<string> p = make_shared<string>("a"); cout << "p use_count:" << p.use_count() << endl; string *p1 = p.get(); cout << "*p:" << *p << ",*p1:" << *p1 << endl; cout << "p use_count:" << p.use_count() << endl; auto p2 = p; cout << "p use_count:" << p.use_count() << endl; shared_ptr<string> p3(p); cout << "p use_count:" << p.use_count() << endl; shared_ptr<string> p4 = make_shared<string>("b"); p3 = p4; cout << "p use_count:" << p.use_count() << endl; p2.~shared_ptr();//... cout << "p use_count:" << p.use_count() << endl; if(p.unique()){ cout << "p is unique"; } } p use_count:1 *p:a,*p1:a p use_count:1 p use_count:2 p use_count:3 p use_count:2 p use_count:1 p is unique
发表评论
-
条件变量
2018-12-03 13:43 404#include <iostream> #i ... -
std::function
2018-08-12 19:23 415#include <iostream> #i ... -
emplace_back
2018-08-02 07:40 512https://blog.csdn.net/xiaolewen ... -
输出类名
2014-02-20 21:51 535#include <iostream> us ... -
c++11应该使用的特性
2013-07-27 11:09 721http://blog.jobbole.com/44015/ -
random(new)
2013-07-21 12:00 753#include<iostream> #in ... -
tuple(new)
2013-07-15 22:11 548#include <tuple> #incl ... -
qtcreator c++11
2013-07-15 22:10 840在.pro里面:QMAKE_CXXFLAGS += -std= ... -
weak_ptr
2013-04-20 16:07 733unique_ptr(定义在中)提供了一种严格的语义上的所有权 ... -
c++ lambda
2013-03-24 11:26 930#include <iostream> #i ... -
定长容器
2013-03-02 12:17 729#include<iostream> #in ... -
数组(new)
2012-10-30 22:00 611#include <iostream> us ... -
vector(new)
2012-10-21 15:05 753#include<iostream> #in ... -
auto(new)
2012-10-20 14:28 596#include<iostream> #in ... -
const
2012-10-08 23:57 1112#include<iostream> usi ... -
变量命名
2012-10-05 09:33 656#include<iostream> usi ... -
声明与定义
2012-10-04 11:33 654extern int i;//声明但未定义 int j;//声 ... -
初始化(new)
2012-10-04 11:16 1151#include<iostream> usi ... -
数字类型
2012-10-02 11:46 565我怕我翻译的不够专业,有些地方就用原文了,反正我是看懂了! 我 ... -
gcc4.7.2安装
2012-09-24 17:44 11151gcc4.7.2编译方法: 推荐第一种 1.简化版: htt ...
相关推荐
shared_ptr只能对new的内存操作.基本知识。
最近看代码,智能指针用的比较多,自己平时用的少,周末自己总结总结。...std::shared_ptr<Test> p1(new Test); std::shared_ptr<Test> p2(new Test); p1 = p2; (4) 引用计数加一/减一操作是原子性的,所
C++智能指针shared_ptr分析 概要: shared_ptr是c++智能指针中适用场景多,功能实现较多的智能指针。它采取引用计数的方法来实现释放指针所指向的资源。下面是我代码实现的基本功能。 实例代码: template class ...
通过 shared_ptr 的构造函数,可以让 shared_ptr 对象托管⼀个 new 运算符返回的指针,写法如下: 此后,ptr 就可以像 T* 类型的指针⼀样使⽤,即 *ptr 就是⽤ new 动态分配的那个对象。 多个 shared_ptr 对象可以...
C++智能指针:shared_ptr⽤法详解 C++智能指针:shared_ptr⽤法详解 shared_ptr是C++11⾥的新特性,其包装了new操作符在堆上分配的动态对象。如: shared_ptr<int> sp1(new int(100)); //相当于 //int *sp1=new int...
int main() { std::shared_ptr<Person> p1(new Person(1));// Person(1)的引⽤计数为1 std::shared_ptr<Person> p2 = std::make_shared(2); p1.reset(new Person(3));// ⾸先⽣成新对象,然后引⽤计数减1,引⽤计数...
C++智能指针shared_ptr讲解与使⽤ ⼿动管理的弊端 在简单的程序中,我们不⼤可能忘记释放 new 出来的指针,但是随着程序规模的增⼤,我们忘了 delete 的概率也随之增⼤。在 C++ 中 new 出来的指针,赋值意味着引⽤的...
和 shared_ptr 指针最⼤的不同之处在 于,unique_ptr 指针指向的堆内存⽆法同其它 unique_ptr 共享,也就是说,每个 unique_ptr 指针都独⾃拥有对其所指堆内存空间的所有 权。 这也就意味着,每个 unique_ptr 指针...
也可以使⽤直接初始化的⽅式 shared_ptr<int> pint(new int(100)) 来创建⼀个 shared_ptr 并初始化,但是由于 shared_ptr 定义 的构造函数是 explicit 的,因此不能使⽤ shared_ptr<int> pint = new int(100) 来...
shared_ptr<std::exception> sp1(new bad_exception("error")); 02. shared_ptr<bad_exception> sp2 = dynamic_pointer_cast<bad_exception>(sp1); 03. shared_ptr<std::exception> sp3 = static_pointer_cast(sp2)...
当shared计数为0时,则证明所有指向同⼀处资源的shared_ptr们全都释放了,则随即释放该资源(哦,还会释放new出来的 SharedPtrControlBlock)。 //shared计数放在这个结构体⾥⾯,实际上结构体⾥还应该有另⼀个weak...
一、使用方法: 1、 将shared_ptr.hpp,shared_ptr.cpp加入 到项目工程中; 2、 如果不需要支持多线程,可以在 "shared_ptr.hpp"文件最开始处...2、 new使用方法上不太标准,如:shared_ptr<B> pb=new(ydsh,(B*)0) B;
分别是shared_ptr、weak_ptr和unique_ptr 智能指针的作⽤ 智能指针可以帮助我们管理动态分配的堆内存,减少内存泄漏的可能性 ⼿动管理堆内存有引起内存泄漏的可能,⽐如这段代码 try { int* p = new int; // Do ...
和 shared_ptr 指针最⼤的不同之处在 于,unique_ptr 指针指向的堆内存⽆法同其它 unique_ptr 共享,也就是说,每个 unique_ptr 指针都独⾃拥有对其所指堆内存空间的所有 权。 这也就意味着,每个 unique_ptr 指针...
和 shared_ptr 指针最⼤的不同之处在 于,unique_ptr 指针指向的堆内存⽆法同其它 unique_ptr 共享,也就是说,每个 unique_ptr 指针都独⾃拥有对其所指堆内存空间的所有 权。 这也就意味着,每个 unique_ptr 指针...
#include <boost/shared_ptr.hpp> class CBase: public boost::enable_shared_from_this<CBase> { public: virtual void f(){}//必须有个虚函数才能向上向下转换。 } typedef boost::shared_ptr<CBase> CBasePtr; ...
⼆、智能指针类型 ⼆、智能指针类型 智能指针在C++11版本之后提供,包含在头⽂件中,标准命名std空间下,有auto_ptr、shared_ptr、weak_ptr、unique_ptr四 种,其中auto_ptr已被弃⽤。 :拥有严格对象所有权语义的...
先看auto_ptr,看⼀个⼩例⼦: auto_ptr<string> films[2] = { auto_ptr<string> (new string("Fowl Balls")), auto_ptr<string> (new string("Duck Walks")) }; auto_ptr<string> pwin; pwin = films[0];//注:...
一、简介 由于 C++ 语言没有自动...包括:std::auto_ptr、boost::scoped_ptr、boost::shared_ptr、boost::scoped_array、boost::shared_array、boost::weak_ptr、boost:: intrusive_ptr。你可能会想,如此多的智能指
C++ 中有四种智能指针:auto_pt、unique_ptr、shared_ptr、weak_ptr 其中后三个是 C++11 ⽀持,第⼀个已经被 C++11 弃⽤且被 unique_prt 代替,不推荐使⽤。下⽂将对其逐个说明。 std::auto_ptr 在这个年代讨论 std...