《Boost标准库完全开发指南》第三章-内存管理

来源:互联网 发布:sigma.js 示例 编辑:程序博客网 时间:2024/04/28 13:25

1、使用boost智能指针只需要引用头文件#include<boost/smart_ptr.hpp>。
2、与auto_ptr不同,scoped_ptr只在本作用域使用,不能转让,auto_ptr在c++11已被声明废弃,改用unique_ptr。

3、scoped_ptr不支持比较,reset函数不提倡使用, scoped_ptr不允许拷贝和赋值,除了支持*和->,没有其他操作符,不自增或自减,不能作为容器的元素存储。

4、根据c++11的标准,unique_ptr既可以代理new也可以代理new[ ]创建的对象,结合了scoped_ptr和scoped_array二者的功能,unique也不可以拷贝和赋值,用法和scoped_ptr一样。但是 unique_ptr有三个额外的功能:(1)可以像原始指针一样比较;(2)可以像shared_ptr一样定制删除器;(3)可以安全地放入标准容器;

有一个例外是unique_ptr支持新的转移语义: unique_ptr<int> up = std::move(another_ptr);

5、scoped_array重载了[],可以使用ptr[i] 访问数组元素,但是不提供索引的范围检查,超过动态数组大小的索引或者是负数索引将引发未定义行为。使用unique_ptr代替scoped_array声明时的格式 unique_ptr<int[]> up(new int[10]);  一般不推荐使用scoped_array,推荐改用vector。

6、shared_ptr使用dynamic_pointer_cast 和static_pointer_cast转换 shared_ptr指针。

7、shared_ptr提供基本的线程安全保证,一个shared_ptr可以被多个线程安全的读取,但是其他的访问形式结果是未定义的。

shared_ptr<int> spi (new int(10));assert(spi.unique());//trueshared_ptr<int> spi2 = spi;cout<<"spi.use_count() = "<<spi.use_count()<<endl;//2cout<<"spi2.use_count() = "<<spi2.use_count()<<endl;//2shared_ptr<string> sps = make_shared<string>("hello");cout<<"*sps = "<<*sps<<endl;//hellocout<<"sps.use_count() = "<<sps.use_count()<<endl;//1typedef vector<shared_ptr<int> > vect_sp_int;vect_sp_int v(10); int i = 0;for (vect_sp_int::iterator iter = v.begin(); iter != v.end(); ++iter){*iter = make_shared<int>(++i);cout<<*(*iter)<<", ";}

8、shared_ptr 适用于桥接模式

//桥接模式 pimpl , handle/body  usageclass Sample{public:Sample();class impl;void print();private:shared_ptr<impl> spi_;};class Sample::impl{public:void print(){cout<<"impl print"<<endl;}};Sample::Sample():spi_(new impl)//构造Sample同时也把impl构造{}void Sample::print(){spi_->print();}void test_sample(){Sample s;s.print();}

9、shared_ptr应用于工厂模式

//应用于工厂模式class abstract_base{public:virtual void f()= 0;virtual void g() = 0;protected:virtual ~abstract_base() {}};class impl_derived: public abstract_base{public:void f() { cout<< "impl_derived f"<<endl;}void g() { cout<< "impl_derived g"<<endl;}};shared_ptr<abstract_base> create(){return shared_ptr<abstract_base>(new impl_derived);}void test_factory_model(){shared_ptr<abstract_base> sp = create();sp->f();sp->g();}
10、 shared_ptr适用定制删除器的用法

//socket

socket* s = open_socket();// close_socket() to close

shared_ptr<socket> sps(s, close_socket); // or shared_ptr<socket> sps(s, &close_socket); 

//file

shared_ptr<FILE> spf(fopen("c://test.txt"), fclose)

//定制删除器的高级用法,退出作用域时调用任意函数void any_function(void* p){cout<<"some operate"<<endl;}void test_exit_any_func(){shared_ptr<void> sp((void*)0, any_function);}

11、关于weak_ptr的作用,主要是获得对资源的监控权,适用use_count可以观察资源的引用计数,调用expired()判断是否引用为空,示例如下

void weak_ptr_usage(){shared_ptr<int> spi(new int(10));assert(spi.use_count() == 1);weak_ptr<int> wpi(spi);assert(wpi.use_count() == 1);if (!wpi.expired()){shared_ptr<int> spi2 = wpi.lock();*spi2 = 100;assert(wpi.use_count() == 2);}assert(wpi.use_count() == 1);// keep to 1wpi.reset();assert(wpi.expired());assert(!wpi.lock());}

12、pool的用法

//#include <boost/pool/pool.hpp>void pool_usage(){pool<> pl(sizeof(int));int* p = static_cast<int*>(pl.malloc());assert(pl.is_from(p));pl.free(p);for (int i = 0; i < 10 ; ++i){pl.ordered_malloc(10);}}

13、object_pool的用法

#include <boost/pool/object_pool.hpp>class demo_cls{public:int a, b, c;demo_cls(int x =1, int y =2, int z = 3): a(x), b(y), c(z){}};void object_pool_usage(){object_pool<demo_cls> op;demo_cls *p = op.malloc();assert(op.is_from(p));p = op.construct(7,8,9);object_pool<string> pls;for (int i = 0; i < 10; i++){string *s = pls.construct("hello, object_pool");cout<<*s<<endl;}}

14、singleton_pool的用法

//singleton_pool_usage#include <boost/pool/singleton_pool.hpp>struct pool_tag {};typedef singleton_pool<pool_tag, sizeof(int)> spl;void singleton_pool_usage(){int* p = (int*)spl::malloc();assert(spl::is_from(p));spl::release_memory();}






0 0
原创粉丝点击