• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

简单说说STL的内存管理

c语言 搞代码 4年前 (2022-01-06) 25次浏览 已收录 0个评论

将其描述为空间配置器,理由是allocator可以将其它存储介质(例如硬盘)做为stl 容器的存储空间。由于内存是allocator管理的主要部分,因此,本文以STL内存管理为出发点介绍allocator

1. 概述
STL Allocator是STL的内存管理器,也是最低调的部分之一,你可能使用了3年stl,但却不知其为何物。

STL标准如下介绍Allocator
the STL includes some low-level mechanisms for allocating and deallocating memory. Allocators are very specialized, and you can safely ignore them for almost all purposes. Allocators encapsulate allocation and deallocation of memory. They provide a low-level interface that permits efficient allocation of many small objects; different allocator types represent different schemes for memory management.
将其描述为空间配置器,理由是allocator可以将其它存储介质(例如硬盘)做为stl 容器的存储空间。由于内存是allocator管理的主要部分,因此,本文以STL内存管理为出发点介绍allocator。

Allocator就在我们身边,通常使用STL的方式:
#include
std::vector Array(100);

本质上,调用的是:

#include
std::vector<int, std::allocator> Array(100);
std::allocator就是一个简单的Allocator

2. 使用
针对不同的应用场合,STL中实现了不同的Allocator,如下(gcc-3.4:http://www.cs.huji.ac.il/~etsman/Docs/gcc-3.4-base/libstdc++/html/20_util/allocator.html):

__gnu_cxx::new_allocator Simply wraps ::operator new and ::operator delete.
__gnu_cxx::malloc_allocator Simply wraps malloc and free. There is also a hook for an out-of-memory handler
__gnu_cxx::debug_allocator A wrapper around an arbitrary allocator A. It passes on slightly increased size requests to A, and uses the extra memory to store size information.
__gnu_cxx::__pool_alloc A high-performance, single pool allocator. The reusable memory is shared among identical instantiations of this type.
__gnu_cxx::__mt_alloc A high-performance fixed-size allocatorthat was initially developed specifically to suit the needs of multi threaded applications
__gnu_cxx::bitmap_allocato A high-performance allocator that uses a bit-map to keep track of the used and unused memory locations

例如,在多线程环境下,可以使用:

代码如下:
#include  
#include  
std::vector<int, __gnu_cxx::__mt_alloc> Array(100); 

3.一个简单的Allocator实现
我们可以实现自己的allocator

代码如下:
#include  

template 
class my_allocator : public std::allocator 

public: 
typedef std::allocator base_type; 

// 必须要重新定义 
template 
struct rebind 

typedef my_allocator other; 
}; 
// 内存的分配与释放可以实现为自定义的算法 
pointer allocate(size_type count) 
{  
return (base_type::allocate(count)); 

void deallocate(pointer ptr, size_type count) 
{  
base_type::deallocate(ptr, count); 

 
// 构造函数 
my_allocator() 
{} 

my_allocator(my_allocator const&) 
{} 

my_allocator& operator=(my_allocator const&) 
{来源gaodai$ma#com搞$代*码*网  
return (*this); 
 } 

template 
my_allocator(my_allocator const&) 
{} 

template 
my_allocator& operator=(my_allocator const&) 
{  
return (*this); } 
};  

以上就是简单说说STL的内存管理的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:简单说说STL的内存管理

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址