boost中内存池该如何用?
大家好:
看了好多boost内存池的文章,但都是说先要指定固定大小的内存块给内存池
我的需求是这样的:
char *m_msg = new char[m_msgSize];// 程序中会频繁的申请和释放m_msgSize大小的内存,m_msgSize大小每次都不一样
不知道我这样的需求用boost该如何实现?
[解决办法]
http://www.cppprog.com/boost_doc/libs/pool/doc/interfaces.html
- C/C++ code
poolThe pool interface is a simple Object Usage interface with Null Return.pool 接口 是一个简单的对象用法接口,它采用空值返回。Example:例子:void func(){ boost::pool<> p(sizeof(int)); for (int i = 0; i < 10000; ++i) { int * const t = p.malloc(); ... // Do something with t; don't take the time to free() it // 使用 t,不用关心何时 free() 它 }} // on function exit, p is destroyed, and all malloc()'ed ints are implicitly freed // 函数退出时,p 被销毁,所有被 malloc() 的整数都被暗中释放