简单说说STL的内存管理

 更新时间:2013年09月14日 09:25:15   作者:  
<STL 源码剖析>将其描述为空间配置器,理由是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.
<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++实现秒表功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • 盘点分析C语言中少见却强大的字符串函数

    盘点分析C语言中少见却强大的字符串函数

    这篇文章主要为大家盘点及分析C语言中少见却强大的字符串函数,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-02-02
  • C++文件输入输出fstream使用方法

    C++文件输入输出fstream使用方法

    C++标准库提供了<fstream>头文件,其中包含了用于文件输入输出的相关类和函数,本文将详细介绍<fstream>头文件的使用方法,包括函数原型、打开文件、读取和写入文件、以及错误处理等注意事项,感兴趣的朋友跟随小编一起看看吧
    2023-10-10
  • C++中基本的输入输出函数使用指南

    C++中基本的输入输出函数使用指南

    这篇文章主要介绍了C++中基本的输入输出函数使用指南,是C++入门学习中的基础知识,需要的朋友可以参考下
    2015-09-09
  • C++数据结构与算法的基础知识和经典算法汇总

    C++数据结构与算法的基础知识和经典算法汇总

    终是到了标志着大二结束的期末考试了,对于《算法设计与分析》这门课,我需要总结一下学过的所有算法的思想以及老师补充的关于两个复杂度和递归的概念思想,以及更深层次的理解,比如用画图的方式表达出来,我觉得可以用博客记录总结一下,分享给大家,希望能有所帮助
    2022-05-05
  • C++ NFS挂载及挂载命令

    C++ NFS挂载及挂载命令

    这篇文章主要介绍了C++ NFS挂载,文中给大家提到了挂载NFS时常用的命令,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-12-12
  • Qt实现密码显示按钮

    Qt实现密码显示按钮

    这篇文章主要为大家详细介绍了Qt实现密码显示按钮,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • 深入探讨C++父类子类中虚函数的应用

    深入探讨C++父类子类中虚函数的应用

    本篇文章是对C++父类子类中虚函数的使用进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • c++实现俄罗斯方块游戏代码

    c++实现俄罗斯方块游戏代码

    大家好,本篇文章主要讲的是c++实现俄罗斯方块游戏代码,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-01-01
  • C++联合体union用法实例详解

    C++联合体union用法实例详解

    这篇文章主要介绍了C++联合体union用法,较为详细的分析了C++中联合体的概念、实用技巧及相关注意事项,需要的朋友可以参考下
    2015-05-05

最新评论