设为首页 加入收藏

TOP

c++ 11学习笔记--智能指针(一)
2015-07-20 18:01:19 来源: 作者: 【 】 浏览:10
Tags:学习 笔记 智能 指针
C++ 98的 std::auto_ptr已经被彻底遗弃了,取而代之的是unique_ptr、shared_ptr与weak_ptr。大部分时候我们自己手动申请内存方式内存都是没有问题的,问题是如果程序很大了之后,一个复杂的对象,多次拷贝的代价非常高,很多地方都会使用到,只存在一份拷贝显然是最好的,这个时候对象生命周期的管理就会很复杂,所以c++引入了智能指针。
?
?
?
任何事物都会有两面性。
?
?
?
Shared_ptr
?
摘录于Effective C++, 3rd Edition, Item 17: 在 standalone statements(独立语句)中将 new 出来的 objects(对象)存入 smart pointers(智能指针)
比如
?
复制代码
class Widget {
public:
? ? Widget() {
? ? ? ? cout << "construct Widget!!!" << endl;
? ? }
? ? ;
? ? ~Widget() {
? ? ? ? cout << "destruct Widget!!!" << endl;
? ? }
? ? ;
};
?
?
int priority()
{
? ? cout << "22222" << endl;
? ? return 0;
}
?
void processWidget(int priority,std::shared_ptr pw)
{
? ?cout << "111111" << endl;
?
}
?
int main()
{
? ? processWidget(priority(),std::shared_ptr(new Widget));
? ??
? ? return 0;
}?
复制代码
processWidget运行的过程应该是
?
1. ? ? New Widget
?
2. ? ? ? ? ? shared_ptr constructor
?
3. ? ? ? ? Priority()
?
我在llvm上测试的结果也是这个执行顺序
?
也许某些编译器上可能执行的顺序是
?
? New Widget
? Priority() //发生异常
? shared_ptr constructor
就有可能有内存泄露
?
?
?
所以最好的办法还是应该把new widget提到外面来写,所以好的编码习惯很重要。
?
?
?
?
?
2.循环引用
?
复制代码
#include
?
using namespace std;
class A;
class B;
typedef std::shared_ptr APtr;
typedef std::shared_ptr BPtr;
?
class A {
public:
? ? BPtr b;
? ? ~A () {
? ? ? ? ?cout << "A released" << endl;
? ? }
};
?
class B {
public:
? ? APtr a;
? ? ~B () {
? ? ? ? cout << "B released" << endl;
? ? }
};
?
int main () {
? ? APtr a(new A());
? ? BPtr b(new B());
? ??
? ? a->b = b; // 1
? ? b->a = a; // 2
? ??
? ? ?cout << "over!!" << endl;
? ? return 0;
}
复制代码
要解决这个问题就需要引入一个弱引用的智能指针了weak_ptr
?
复制代码
class A;
class B;
?
typedef std::shared_ptr BPtr;
typedef std::weak_ptr BWeakPtr;
?
class A {
public:
? ? BWeakPtr b; // 注意这里
? ? ~A () {
? ? ? ? cout << "A released" << endl;
? ? }
? ??
};
?
class B {
public:
? ? AWeakPtr a; // 注意这里
? ? ~B () {
? ? ? ? cout << "B released" << endl;
? ? }
? ??
};
?
int main () {
? ? APtr a(new A());
? ? BPtr b(new B());
? ??
? ? a->b = b;
? ? b->a = a;
? ??
? ? return 0;?
}
复制代码
这两种指针其实和oc里面的 strong, weak非常相识。
?
?
?
?weak_ptr的另外一种用法
?
使用情景:当类对象被 shared_ptr 管理时,需要在类自己定义的函数里把当前类对象作为参数传给其他函数时,这时需要传递一个 shared_ptr ,否则就不能保持 shared_ptr 管理这个类对象的语义(因为有一个 raw pointer 指向这个类对象,而 shared_ptr 对类对象的这个引用没有计数,很有可能 shared_ptr 已经把类对象资源释放了,而那个调用函数还在使用类对象——显然,这肯定会产生错误)。《摘录:http://blog.csdn.net/zhongguoren666/article/details/8617436》
?
?
?
直接看官网的例子吧:
?
《http://en.cppreference.com/w/cpp/memory/enable_shared_from_this》
?
?
?
复制代码
#include
#include
?
struct Good: std::enable_shared_from_this
{
? ? std::shared_ptr getptr() {
? ? ? ? return shared_from_this();
? ? }
};
?
struct Bad
{
? ? std::shared_ptr getptr() {
? ? ? ? return std::shared_ptr(this);
? ? }
? ? ~Bad() { std::cout << "Bad::~Bad() called\n"; }
};
?
class CObj: public std::enable_shared_from_this {
? ? friend class CObjMgr;
protected:
? ? CObj() {} ? // 只有CObjMgr可以创建与删除
? ? ~CObj(){}
};
?
?
int main()
{
? ? // Good: the two shared_ptr's share the same object
? ? std::shared_ptr gp1(new Good);
? ? std::shared_ptr gp2 = gp1->getptr();
? ? std::cout << "gp2.use_count() = " << gp2.use_count() << '\n';
? ??
? ? // Bad, each shared_ptr thinks it's the only owner of the object
? ? std::shared_ptr bp1(new Bad);
? ? std::shared_ptr bp2 = bp1
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇泛型编程与C++标准模板库 : 浅谈s.. 下一篇codeforces #259 DIV2 C题 Little..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: