smart_ptr

  • raii ( Resource Acquisition Is Initialization )
  • 智能指针系列的都统称为smart_ptr。包含c++98标准的auto_ptr
  • 智能指针是一个类,通过重载->和*完毕相似原始指针的操作。

    只是由于是类,所以能够做比方内存管理、线程安全之类的工作

  • 智能指针均是自己主动管理内存,不须要显示调用delete

scoped_ptr

  • 与auto_ptr最大的不同,是私有化构造和拷贝构造,使操作权不能转让,所以有强作用域属性,并且指针类自己负责释放
  • 与c++11标准的unique_ptr相比:unique_ptr有很多其它功能,能够像原始指针一样进行比較、像shared_ptr一样定制删除器,也能够安全地放入标准容器。可是少即是多。scope_ptr专注于强调作用域属性。

scoped_array

  • 与scoped_ptr相似,仅仅只是封装的是new[]分配数组。
  • 不是指针。也就没有实现*和->的重载,而是直接[]下标訪问
  • 通常不建议使用scoped_array。它的出现通常往往意味着你的代码中存在着隐患

shared_ptr(重点)

  • boost.smart_ptr中最有价值、最重要的组成部分,也最经经常使用
  • unique()在shared_ptr是指针中唯一全部者时返回true
  • user_count()返回当前指针的引用计数
  • 提供operator<、==比較操作 。使shared_ptr能够被用于set/map标准容器
  • 编写多态指针时。不能使用诸如static_cast< T* >(p.get())的形式。这将导致转型后的指针无法再被shared_ptr正确管理。为了支持这种使用方法,shared_ptr提供内置的转型函数static_pointer_cast< T >()、const_pointer_cast< T >()和dynamic_pointer_cast< T >()
  • 支持operator< < 操作打印指针值,便与调试
  • 有工厂函数
template< class T, calss... Args >
shared_ptr< T> make_shared(Args && ... args);
  • 使用代码案例:
shared_ptr< string > sps(new string("smart"));
assert(sps->size() == 5);
shared_ptr< string> sp = make_shared< string > ("make_shared");
shared_ptr< vector< int >> spv = make_shared< vector< int >> (10,2);
assert(spv->size() == 10);
  • 应用于标准容器代码案例:
#include < boost/make_shared.hpp>
int main()
{
typedef vector< shared_ptr< int>> vs;
vs v(10);
int i=0;
for (vs::iterator pos = v.begin(); pos != v.end(); ++pos)
{
(*pos) = make_shared< int>(++i);
cout << *(*pos) << ",";
}
cout << endl;
shared_ptr < int > p = v[9];
*p = 100;
cout << *v[9] << endl;
}

shared_array

  • 通经常使用shared_ptr< std::vector>或者std::vector< shared_ptr>取代

weak_ptr(重点)

  • 未重载*和->。最大的作用是协助shared_ptr。观測资源使用情况
  • 代码演示样例:
int testWeakPtr()
{
boost::shared_ptr<int> sp(new int(10));
assert(sp.use_count() == 1); boost::weak_ptr<int> wp(sp);
assert(wp.use_count() == 1); if (!wp.expired())
{
boost::shared_ptr<int> sp2 = wp.lock();
*sp2 = 100;
assert(sp2.use_count() == 2);
assert(wp.use_count() == 2);
} assert(wp.use_count() == 1);
sp.reset();
assert(wp.expired());
assert(!wp.lock()); return 0;
}
  • 作用是打破循环引用,代码演示样例:
// 循环引用的情形
class node
{
public:
boost::shared_ptr<node> next;
~node()
{
cout << "delete node" << endl;
}
}; int testWeakPtrRecycleWrong()
{
auto n1 = boost::make_shared<node>();
auto n2 = boost::make_shared<node>(); n1->next = n2;
n2->next = n1; assert(n1.use_count() == 2);
assert(n2.use_count() == 2); return 0; // 循环引用,析构异常。程序退出时仍未析构
} // 避免循环引用的情形,主要就是node里的shared_ptr换成weak_ptr
class node1
{
public:
boost::weak_ptr<node1> next;
~node1()
{
cout << "delete node1" << endl;
}
}; int testWeakPtrRecycleRight()
{
auto n1 = boost::make_shared<node1>();
auto n2 = boost::make_shared<node1>(); n1->next = n2;
n2->next = n1; assert(n1.use_count() == 1);
assert(n2.use_count() == 1); if (!n1->next.expired())
{
// 调用lock()获得强引用,计数加1
auto n3 = n1->next.lock();
assert(n2.use_count() == 2);
}
assert(n2.use_count() == 1);
return 0;
}

intrusive_ptr(略)

pool(pool后缀的都是重点)

  • pool管理内存。仅仅能针对基础类型POD(Plain Old Data),由于不调用对象的构造函数。除非特殊情况。不须要自己释放
  • 调用演示样例代码:
int testPool()
{
pool<> pl(sizeof(int));
int *p = static_cast<int*>(pl.malloc()); // void*->int*
assert(pl.is_from(p)); pl.free(p);
for (int i = 0; i < 100;i++)
{
pl.ordered_malloc(10);
}
return 0;
}

object_pool

  • pool的子类。实现对类实例(对象)的内存池。会调用析构函数正确释放资源
  • 演示样例代码:
struct demo_class
{
public:
int a, b, c;
demo_class(int x = 1, int y = 2, int z = 3) : a(x), b(y), c(z) {}
~demo_class()
{
cout << "destruct" << endl;
}
}; int testObjPool()
{
object_pool<demo_class> pl;
demo_class *p = pl.malloc();
assert(pl.is_from(p)); // malloc 创建时内存并未初始化
assert(p->a!= 1 || p->b != 2 || p->c != 3); p = pl.construct(); // boost自带的construct仅仅能支持3个及以内的參数调用
p = pl.construct(7, 8, 9);
assert(p->a == 7); object_pool<string> pls;
for (int i = 0; i < 10; i++)
{
string *ps = pls.construct("hello object_pool");
cout << *ps << endl;
}
return 0;
}

析构会调用三次。malloc和两次construct均须要调用析构。是object_pool推断出了作用域自己主动调整的。

singleton_pool

  • 基础类型静态单件(包含基础指针类型)。提供线程安全
  • 演示样例代码
struct pool_tag{
int tag;
};
typedef singleton_pool<pool_tag, sizeof(pool_tag*)> spl;
int testSingletonPool()
{
pool_tag **p = (pool_tag **)spl::malloc();
assert(spl::is_from(p)); pool_tag ptag;
ptag.tag = 3;
*p = &ptag; cout << "testSingletonPool : " << (*p)->tag << endl;
spl::release_memory();
return 0;
}

pool_alloc

  • 提供对标准容器类型的内存分配器,当分配失败时能够抛异常。可是除非特别需求。应该使用stl自带的。假设要用pool_alloc需经过细致測试。保证与容器能够正常工作。

boost的内存管理的更多相关文章

  1. boost之内存管理

    内存管理一直是令C++程序员最头疼的工作,C++继承了C那高效而又灵活的指针,使用起来稍微不小心就会导致内存泄露.野指针.越界访问等访问.虽然C++标准提供了只能指针std::auto_ptr,但是并 ...

  2. (六)boost库之内存管理shared_ptr

    (六)boost库之内存管理shared_ptr 1.shared_ptr的基本用法 boost::shared_ptr<int> sp(new int(10)); //一个指向整数的sh ...

  3. Object-C内存管理的理解总结

    今天看到了OC的内存管理这块,觉得很亲切. 自己的习惯是尽量自己掌控程序的空间和时间,有点强迫症的感觉.用C和C++做项目的时候,时时刻刻都在操心这new和delete的配对使用和计数,学习stl和b ...

  4. [基础] C++与JAVA的内存管理

    在内存管理上(总之一句话——以后C++工程,一定要用智能指针!) 1.同是new一个对象,C++一定得手动delete掉,而且得时刻记住能delete的最早时间(避免使用空指针).JAVA可以存活于作 ...

  5. C++内存管理学习笔记(5)

    /****************************************************************/ /*            学习是合作和分享式的! /* Auth ...

  6. STL内存管理

    1. 概述 STL Allocator是STL的内存管理器,也是最低调的部分之一,你可能使用了3年stl,但却不知其为何物. STL标准如下介绍Allocator the STL includes s ...

  7. C++内存管理变革(6):通用型垃圾回收器 - ScopeAlloc

    本文已经迁移到:http://cpp.winxgui.com/cn:a-general-gc-allocator-scopealloc C++内存管理变革(6):通用型垃圾回收器 - ScopeAll ...

  8. C++中的垃圾回收和内存管理

    最开始的时候看到了许式伟的内存管理变革系列,看到性能测试结果的时候,觉得这个实现很不错,没有深入研究其实现.现在想把这个用到自己的一个项目中来,在linux下编译存在一些问题,所以打算深入研究一下. ...

  9. 【cocos2d-x 3.x 学习笔记】对象内存管理

    内存管理 内存管理一直是一个不易处理的问题.开发人员必须考虑分配回收的方式和时机,针对堆和栈做不同的优化处理,等等.内存管理的核心是动态分配的对象必须保证在使用完成后有效地释放内存,即管理对象的生命周 ...

随机推荐

  1. [JOYOI] 1096 数字组合

    题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接交谈了.幸运的是 ...

  2. [MVC]Ajax辅助方法

    在开始使用Ajax辅助方法前,必须在页面中载入jQuery以及jquery.unobtrusive-ajax.js文件才能正常执行. 为了让网站载入适当的JS函数库,必须先让Layout页面载入适当的 ...

  3. 2016年工作中遇到的问题41-50:Dubbo注册中心奇葩问题,wifi热点坑了

    41.获得JSON中的变量.//显示json串中的某个变量,name是变量名function json(json,name){ var jsonObj = eval(json); return jso ...

  4. vim 系列

    http://blog.csdn.net/laogaoav/article/details/17370269 非常不错

  5. 跟初学者学习IbatisNet第三篇

    这一章我们主要介绍一下IbatisNet里面的动态sql语句的运用,比如有时候我们想进行模糊查询,参数是动态加入的.或者要实现top n ,order by ,分页等功能的时候,我们就不得不用动态拼接 ...

  6. 深刻理解下js的prototype

    参考 http://aralejs.org/class/docs/competitors.html, http://www.iteye.com/topic/248933,http://www.cnbl ...

  7. jquery怎么获得ul中li的个数

  8. Android滚动页面位置指示器:CircleIndicator

     Android滚动页面位置指示器:CircleIndicator CircleIndicator是github上的一个开源的用于页面滚动时候的位置指示器,指示当前页面在总的页面中的位置和前后位置 ...

  9. Java多线程干货系列—(二)synchronized

    原文地址:http://tengj.top/2016/05/03/threadsynchronized2/ <h1 id="前言"><a href="# ...

  10. 【BZOJ2982】combination(Lucas定理)

    题意:求C(n,m) n,m<=200000000 思路:c(n,m)=c(n mod mo,m mod mo)*c(n div mo,m div mo) mod mo (n>=mo或m& ...