C++的boost学习--内存管理

来源:互联网 发布:淘宝助理5.5在哪下载 编辑:程序博客网 时间:2024/06/15 00:17
boost库的内存处理
智能指针
1,scoped_ptr
不允许拷贝,赋值,只能在被声明的作用域中使用,不需要使用delete释放资源,自动释放资源,可以获得与原始指针同样的速度。
#include<iostream>
#include<boost\smart_ptr.hpp>
using namespace std;
void usageScopedPtr();
class Test{
public:
Test(const char*p){cout<<"new test"<<p<<endl;}
~Test(){cout<<"close"<<endl;}
};
int main(){
usageScopedPtr();


    return 0;
}
void usageScopedPtr(){
boost::scoped_ptr<string> sp(new string("what"));
cout<<*sp<<endl;
cout<<sp->size()<<endl;
boost::scoped_ptr<Test> spt(new Test("hi you"));
}
2,auto_ptr指针
用法几乎和scoped_ptr一样,区别就是auto_ptr会被转移,而scoped_ptr不会被转移,编译期会报错。
scoped_ptr只能在被声明的作用域使用,不能转移,在代码后期维护阶段很重要。
#include<iostream>
#include<boost\smart_ptr.hpp>
using namespace std;
void usageScopedPtr();
void usageAutoPtr();
class Test{
public:
Test(const char*p){cout<<"new test"<<p<<endl;}
~Test(){cout<<"close"<<endl;}
};
int main(){
usageScopedPtr();


    return 0;
}
void usageScopedPtr(){
boost::scoped_ptr<string> sp(new string("what"));
cout<<*sp<<endl;
cout<<sp->size()<<endl;
boost::scoped_ptr<Test> spt(new Test("hi you"));
cout<<spt.get()<<endl;
}
void usageAutoPtr(){
auto_ptr<int> at1(new int[10]);
boost::scoped_ptr<auto_ptr<int>> spa1(at1);
assert(at1.get() == 0);//断言自动指针已被转移,会被转移的auto_ptr,scoped_ptr的区别之一
//scoped_ptr不被转移
boost::scoped_ptr<int> scp2(new int[2]);
boost::scoped_ptr<boost::scoped_ptr<int>> ppp2(scp2);




}
注意:最后一行代码报错,因为我试图转移scoped_ptr




3,scoped_array
弥补了标准库中,没有指向数组的指针的遗憾
特点(1)构造函数必须是new[],
(2)没有重载*,->因为不是一个普通的指针
(3)析构函数使用的是delete[]
(4)没有begin,end
它和scoped_ptr区别就是它管理的是动态数组,而不是单个动态对象
4,shared_ptr
boost完全开发指南的作者强调这个指针是最重要的,在过去现在未来都是,是一个计数型智能指针,可以安全共享,
5,weak_ptr
shared_ptr的助手,不共享指针,不操作资源,弱指针
6,intrusive_ptr
因为shared_ptr指针能完成百分之九十九的工作了,所以不推荐使用intrusive_ptr指针
pool内存池库
#include<boost\pool\pool.hpp> 
容易使用,但是只能操作简单数据类型,如int,double等
object_pool,解决了这个问题。还有singleton_pool,pool_alloc,,,
#include<iostream>
#include<boost\smart_ptr.hpp>
#include<boost\pool\pool.hpp>
using namespace std;
void usageScopedPtr();
void usageAutoPtr();
void usageScopedArray();
void usagePool();
class Test{
public:
Test(const char*p){cout<<"new test"<<p<<endl;}
~Test(){cout<<"close"<<endl;}
};
int main(){
usagePool();
    return 0;
}
void usageScopedPtr(){
boost::scoped_ptr<string> sp(new string("what"));
cout<<*sp<<endl;
cout<<sp->size()<<endl;
boost::scoped_ptr<Test> spt(new Test("hi you"));
cout<<spt.get()<<endl;
}
void usageAutoPtr(){
auto_ptr<int> at1(new int[10]);
boost::scoped_ptr<int> spa1(at1);
assert(at1.get() == 0);//断言自动指针已被转移,会被转移的auto_ptr,scoped_ptr的区别之一
//scoped_ptr不被转移
boost::scoped_ptr<int> scp2(new int[2]);
}
void usageScopedArray(){
int *arr = new int[100];
boost::scoped_array<int> sa(arr);
fill_n(&sa[0],100,5);
sa[10] = sa[20]+sa[30];
}
void usagePool(){
boost::pool<> pl(sizeof(int));//一个int型的内存池
int *p = (int*)pl.malloc();//void*|强转成int*
assert(pl.is_from(p));
pl.free(p);//释放资源
for(int i=0;i<100;++i){
pl.ordered_malloc(10);//分配大量资源
}












智能指针最好用的是shared_ptr,内存池最好用的是object_pool,当然具体情况具体分析
0 0
原创粉丝点击