簡單說說STL的內(nèi)存管理
1. 概述
STL Allocator是STL的內(nèi)存管理器,也是最低調(diào)的部分之一,你可能使用了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可以將其它存儲介質(zhì)(例如硬盤)做為stl 容器的存儲空間。由于內(nèi)存是allocator管理的主要部分,因此,本文以STL內(nèi)存管理為出發(fā)點介紹allocator。
Allocator就在我們身邊,通常使用STL的方式:
#include <vector>
std::vector<int> Array(100);
本質(zhì)上,調(diào)用的是:
#include <vector>
std::vector<int, std::allocator> Array(100);
std::allocator就是一個簡單的Allocator
2. 使用
針對不同的應用場合,STL中實現(xiàn)了不同的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
例如,在多線程環(huán)境下,可以使用:
#include <vector>
#include <mt_allocator.h>
std::vector<int, __gnu_cxx::__mt_alloc<int>> Array(100);
3.一個簡單的Allocator實現(xiàn)
我們可以實現(xiàn)自己的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;
};
// 內(nèi)存的分配與釋放可以實現(xiàn)為自定義的算法
pointer allocate(size_type count)
{
return (base_type::allocate(count));
}
void deallocate(pointer ptr, size_type count)
{
base_type::deallocate(ptr, count);
}
// 構(gòu)造函數(shù)
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語言 volatile與const同時使用應注意的問題
本文標題:簡單說說STL的內(nèi)存管理
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/4139.html
您可能感興趣的文章
- 01-10如何用C語言生成簡單格式的xml
- 01-10C++中簡單讀寫文本文件的實現(xiàn)方法
- 01-10深入解析C++ STL中的常用容器
- 01-10STL list鏈表的用法詳細解析
- 01-10STL常用容器詳細解析
- 01-10STl中的排序算法詳細解析
- 01-10關(guān)于STL中vector容器的一些總結(jié)
- 01-10stl容器set,map,vector之erase用法與返回值詳細解析
- 01-10STL各個容器性能詳細比較
- 01-10關(guān)于STL中l(wèi)ist容器的一些總結(jié)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10delphi制作wav文件的方法