欢迎来到代码驿站!

C代码

当前位置:首页 > 软件编程 > C代码

简单说说STL的内存管理

时间:2022-02-25 10:16:32|栏目:C代码|点击:

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.
<STL 源码剖析>将其描述为空间配置器,理由是allocator可以将其它存储介质(例如硬盘)做为stl 容器的存储空间。由于内存是allocator管理的主要部分,因此,本文以STL内存管理为出发点介绍allocator。

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

本质上,调用的是:

#include <vector>
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<T> Simply wraps ::operator new and ::operator delete.
__gnu_cxx::malloc_allocator<T> Simply wraps malloc and free. There is also a hook for an out-of-memory handler
__gnu_cxx::debug_allocator<T> 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<bool, int> A high-performance, single pool allocator. The reusable memory is shared among identical instantiations of this type.
__gnu_cxx::__mt_alloc<T> 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 <vector> 
#include <mt_allocator.h> 
std::vector<int, __gnu_cxx::__mt_alloc<int>> Array(100); 

3.一个简单的Allocator实现
我们可以实现自己的allocator
复制代码 代码如下:

#include <memory> 

template<class T> 
class my_allocator : public std::allocator<T> 

public: 
typedef std::allocator<T> base_type; 

// 必须要重新定义 
template<class Other> 
struct rebind 

typedef my_allocator<Other> 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<T> const&) 
{} 

my_allocator<T>& operator=(my_allocator<T> const&) 
{  
return (*this); 
 } 

template<class Other> 
my_allocator(my_allocator<Other> const&) 
{} 

template<class Other> 
my_allocator<T>& operator=(my_allocator<Other> const&) 
{  
return (*this); } 
};  

上一篇:详解C++ 转换的非正式分类

栏    目:C代码

下一篇:C++ pair的用法案例详解

本文标题:简单说说STL的内存管理

本文地址:http://www.codeinn.net/misctech/194530.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有