C++ Boost 内存池与智能指针
Pool内存池: 只能开辟常规内存,数据类型为int,float,double,string等。
#include <iostream>
#include <boost/pool/pool.hpp>
#include <boost/pool/object_pool.hpp>
using namespace std;
using namespace boost;
int main(int argc, char const *argv[])
{
boost::pool<> pool(sizeof(int)); // 定义整数内存池
int *ptr[10] = { 0 }; // 定义指针列表
for (int x = 0; x < 10; x++)
{
ptr[x] = static_cast<int *>(pool.malloc()); // 开辟空间并转为指针
if (ptr[x] == nullptr)
cout << "分配空间失败" << endl;
}
// 分别对内存空间赋值
for (int x = 0; x < 10; x++)
*ptr[x] = x;
// 输出数据
for (int x = 0; x < 10; x++)
{
cout << "内存地址: " << &ptr[x] << " 数值: " << *ptr[x] << endl;
}
getchar();
return 0;
}
objectPool 内存池: 该内存池支持对结构体,对象的分配与初始化。
#include <iostream>
#include <string>
#include <boost/pool/pool.hpp>
#include <boost/pool/object_pool.hpp>
using namespace std;
using namespace boost;
struct MyStruct
{
public:
int uuid;
string uname;
int uage;
string usex;
MyStruct(int uuid_,string uname_,int uage_, string usex_)
{
uuid = uuid_; uname = uname_; uage = uage_; usex = usex_;
}
};
// 定义可变参数模板,用来实现接受三个以上的参数
template<typename P, typename ... Args> inline typename P::element_type* construct(P& p, Args&& ... args)
{
typename P::element_type* mem = p.malloc();
new(mem) typename P::element_type( std::forward<Args>(args)...);
return mem;
}
int main(int argc, char const *argv[])
{
boost::object_pool<MyStruct> object;
auto ptr = object.malloc();
// 默认最多只能传递3个参数
//ptr = object.construct(1001,"lyshark",25); // 为构造函数传递参数
//cout << "姓名: " << ptr->uname << endl;
// 接收四个参数写法
auto ref = construct(object, 1001, "lyshark", 24, "男");
cout << "姓名: " << ref->uname << endl;
object.free(ref);
object.free(ptr);
getchar();
return 0;
}
shared_ptr 智能指针:
#include <iostream>
#include <string>
#include <boost/smart_ptr.hpp>
using namespace std;
using namespace boost;
int main(int argc, char const *argv[])
{
// 基本的定义与赋值
boost::shared_ptr<int> int_ptr(new int);
*int_ptr = 1024;
cout << "指针: " << &int_ptr << " 数值: " << *int_ptr << endl;
boost::shared_ptr<string> string_ptr(new string);
*string_ptr = "hello lyshark";
cout << "指针: " << &string_ptr << " 长度: " << string_ptr->size() << endl;
// 拷贝构造的使用
boost::shared_ptr<int> shared_ptr(new int(10)); // 定义指向整数的shared
cout << "持有者: " << shared_ptr.unique() << endl;
boost::shared_ptr<int>shared_copy = shared_ptr; // 实现拷贝
cout << "引用数: " << shared_ptr.use_count() << endl;
shared_ptr.reset(); // 关闭shared的使用
getchar();
return 0;
}
#include <iostream>
#include <string>
#include <boost/smart_ptr.hpp>
using namespace std;
using namespace boost;
class shared
{
private:
boost::shared_ptr<int> ptr;
public:
shared(boost::shared_ptr<int> p_) :ptr(p_){}
void print()
{
cout << "内部 计数: " << ptr.use_count() << " 数值: " << *ptr << endl;
}
};
// 自动拷贝一个对象,所以引用计数会+1
void print_func(boost::shared_ptr<int> ptr)
{
cout << "外部 计数: " << ptr.use_count() << " 数值: " << *ptr << endl;
}
int main(int argc, char const *argv[])
{
boost::shared_ptr<int> ptr(new int(100)); // 定义整数指针
shared s1(ptr), s2(ptr); // 定义两个对象,并初始化
s1.print();
s2.print();
*ptr = 200;
print_func(ptr);
s1.print();
s2.print();
getchar();
return 0;
}
make_shared 工厂函数: 工厂函数常用于初始化特定的指针数据的。
#include <iostream>
#include <string>
#include <vector>
#include <boost/smart_ptr.hpp>
using namespace std;
using namespace boost;
int main(int argc, char const *argv[])
{
// make_shared 工厂函数初始化
boost::shared_ptr<string> string_ptr = boost::make_shared<string>("hello lyshark");
cout << "初始化字符串: " << *string_ptr << endl;
// 应用于标准容器中
typedef std::vector<boost::shared_ptr<int>> vector_ptr; // 定义标准容器类型
vector_ptr vec(10); // 定义拥有十个元素的容器
int x = 0;
for (auto pos = vec.begin(); pos != vec.end(); ++pos)
{
(*pos) = boost::make_shared<int>(++x); // 工厂函数初始化
cout << "输出值: " << *(*pos) << endl; // 输出数据(两次解引用)
}
boost::shared_ptr<int> int_ptr = vec[9]; // 获取最后一个数值
*int_ptr = 100; // 修改最后一个数值
cout << "修改后: " << *vec[9] << endl;
// 第二种输出方式(一次解引用完成)
x = 0;
for (auto& ptr : vec)
{
cout << "输出值: " << *ptr << endl;
}
getchar();
return 0;
}
shared_ptr 桥接模式: 又称为PIMPI模式,仅对外部暴漏最小的细节,内部类实现用一个shared_ptr来保存指针。
#include <iostream>
#include <string>
#include <vector>
#include <boost/smart_ptr.hpp>
using namespace std;
using namespace boost;
class sample
{
private:
class impl;
boost::shared_ptr<impl> ptr;
public:
sample();
void print();
};
class sample::impl
{
public:
void print()
{
cout << "impl print" << endl;
}
};
sample::sample() :ptr(new impl){}
void sample::print()
{
ptr->print();
}
int main(int argc, char const *argv[])
{
sample lsp;
lsp.print();
getchar();
return 0;
}
markshare 实现工厂模式
#include <iostream>
#include <string>
#include <vector>
#include <boost/smart_ptr.hpp>
using namespace std;
using namespace boost;
// 定义基类 全部为虚函数
class abstract
{
public:
virtual void f() = 0;
virtual void g() = 0;
protected:
virtual ~abstract() = default;
};
// 派生类实现虚函数
class impl :public abstract
{
public:
impl() = default;
virtual ~impl() = default;
public:
virtual void f()
{
cout << "class impl f" << endl;
}
virtual void g()
{
cout << "class impl g" << endl;
}
};
// 工厂函数返回基类的shared_ptr;
boost::shared_ptr<abstract> create()
{
return boost::make_shared<impl>();
}
int main(int argc, char const *argv[])
{
auto ptr = create();
ptr->f();
ptr->g();
abstract *q = ptr.get();
q->f();
q->g();
getchar();
return 0;
}
weak_ptr : 配合shared_ptr 作用在于协助shared_ptr 像旁观者一样观察资源的使用情况。
#include <iostream>
#include <string>
#include <vector>
#include <boost/smart_ptr.hpp>
using namespace std;
using namespace boost;
int main(int argc, char const *argv[])
{
boost::shared_ptr<int> ptr(new int(10));
boost::weak_ptr<int> weak(ptr);
// 判断weak_ptr观察的对象是否失效
if (!weak.expired())
{
// 获得一个shared_ptr
boost::shared_ptr<int> new_ptr = weak.lock();
*new_ptr = 100;
}
ptr.reset();
getchar();
return 0;
}
C++ Boost 内存池与智能指针的更多相关文章
- 重写boost内存池
最近在写游戏服务器网络模块的时候,需要用到内存池.大量玩家通过tcp连接到服务器,通过大量的消息包与服务器进行交互.因此要给每个tcp分配收发两块缓冲区.那么这缓冲区多大呢?通常游戏操作的消息包都很小 ...
- Boost内存池使用与测试
目录 Boost内存池使用与测试 什么是内存池 内存池的应用场景 安装 内存池的特征 无内存泄露 申请的内存数组没有被填充 任何数组内存块的位置都和使用operator new[]分配的内存块位置一致 ...
- 必须要注意的 C++ 动态内存资源管理(五)——智能指针陷阱
必须要注意的 C++ 动态内存资源管理(五)——智能指针陷阱 十三.小心使用智能指针. 在前面几节已经很详细了介绍了智能指针适用方式.看起来,似乎智能指针很强大,能够很方便很安全的管理 ...
- c++动态内存管理与智能指针
目录 一.介绍 二.shared_ptr类 make_shared函数 shared_ptr的拷贝和引用 shared_ptr自动销毁所管理的对象- -shared_ptr还会自动释放相关联对象的内存 ...
- 基于C/S架构的3D对战网络游戏C++框架_05搭建系统开发环境与Boost智能指针、内存池初步了解
本系列博客主要是以对战游戏为背景介绍3D对战网络游戏常用的开发技术以及C++高级编程技巧,有了这些知识,就可以开发出中小型游戏项目或3D工业仿真项目. 笔者将分为以下三个部分向大家介绍(每日更新): ...
- [6] 智能指针boost::weak_ptr
[1]boost::weak_ptr简介 boost::weak_ptr属于boost库,定义在namespace boost中,包含头文件 #include<boost/weak_ptr.hp ...
- 智能指针剖析(上)std::auto_ptr与boost::scoped_ptr
1. 引入 C++语言中的动态内存分配没有自动回收机制,动态开辟的空间需要用户自己来维护,在出函数作用域或者程序正常退出前必须释放掉. 即程序员每次 new 出来的内存都要手动 delete,否则会造 ...
- C++智能指针剖析(上)std::auto_ptr与boost::scoped_ptr
1. 引入 C++语言中的动态内存分配没有自动回收机制,动态开辟的空间需要用户自己来维护,在出函数作用域或者程序正常退出前必须释放掉. 即程序员每次 new 出来的内存都要手动 delete,否则会造 ...
- 详解 boost 库智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> 源码分析)
一.boost 智能指针 智能指针是利用RAII(Resource Acquisition Is Initialization:资源获取即初始化)来管理资源.关于RAII的讨论可以参考前面的文章.在使 ...
- Boost智能指针-基础知识
简单介绍 内存管理一直是 C++ 一个比較繁琐的问题,而智能指针却能够非常好的解决问题,在初始化时就已经预定了删除.排解了后顾之忧.1998年修订的第一版C++标准仅仅提供了一种智能指针:std::a ...
随机推荐
- Android WebView 踩坑日记,字体怎么突然变小了???
背景 最近,端内在做 webView 统一的时候,个性签名中的 WebView 替换为 CustomWebView 之后,发现字体突然变小. 一开始不知道是什么原因,通过二分法查找最近的提交,排查之后 ...
- 如临现场的视觉感染力,NBA决赛直播还能这样看?
在6月16日结束的NBA总决赛中,勇士4-2击败凯尔特人,问鼎总冠军!今年的NBA总决赛吸引了众多关注,互联网各大平台的赛事直播气氛也异常热烈. 平台如何既能展现专业的赛事解说,又能与球迷观众深入互动 ...
- LeetCode | 983.最低票价(动态规划)
在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的日子将以一个名为days的数组给出.每一项是一个从 1 到 365 的整数. 火车票有三种不同的销售方式: 一张 ...
- 【网络爬虫学习】Python 爬虫初步
本系列基于 C语言中文网的 Python爬虫教程(从入门到精通)来进行学习的, 部分转载的文章内容仅作学习使用! 前言 网络爬虫又称网络蜘蛛.网络机器人,它是一种按照一定的规则自动浏览.检索网页信息的 ...
- 牛客 | 一起来做题~欢乐赛2 (AK 题解)
补题链接:Here A.新比赛,在眼前. 对于每次猜数和裁判的判断,可以确定一个区间内所有的数都有可能,比如对于样例中(8 +)来说,[ -INT_MIN, 7] 中所有的数都有可能,那么对于每次猜数 ...
- LeetCode75 颜色分类 (三路快排C++实现与应用)
三路快排是快速排序算法的升级版,用来处理有大量重复数据的数组. 主要思想是选取一个key,小于key的丢到左边,大于key的丢到右边,递归实现即可. 具体操作过程参考:https://blog.csd ...
- 深入剖析 RSA 密钥原理及实践
一.前言 在经历了人生的很多至暗时刻后,你读到了这篇文章,你会后悔甚至愤怒:为什么你没有早点写出这篇文章?! 你的至暗时刻包括: 1.你所在的项目需要对接银行,对方需要你提供一个加密证书.你手上只有一 ...
- 【教程】步兵 cocos2dx 加密和混淆
文章目录 摘要 引言 正文代码加密具体步骤代码加密具体步骤测试和配置阶段IPA 重签名操作步骤 总结 参考资料 摘要 本篇博客介绍了针对 iOS 应用中的 Lua 代码进行加密和混淆的相关技术.通过对 ...
- vue中class样式与内联样式
(1):style使用 <div class="score" :style="{ color: colorComputed(item.status) }" ...
- Vue3.0 + Element Plus整合实战
mall-vue3-manage 基于vue3.0 + Element Plus. 整合最新的 Echarts5 强劲的渲染引擎.富文本编辑器 Wangeditor 的后端管理项目. 版本 vue 3 ...