麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > C > 正文

簡單說說STL的內存管理

2020-01-26 15:52:44
字體:
來源:轉載
供稿:網友

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); } 
};  

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 中文字幕在线观看网址 | 国产亚洲精品久久久久久久久久 | 羞羞答答www网站进入 | 大逼逼影院| 黄色av网站在线观看 | 日日摸夜夜添夜夜添牛牛 | 蜜桃免费在线 | 国产精品视频成人 | 亚洲精品7777xxxx青睐 | 禁漫天堂久久久久久久久久 | 精品中文视频 | 亚洲国产精品久久久久久久久久 | 看全色黄大色黄大片女图片 | 久久国产精品久久久久 | 久久国产成人午夜av浪潮 | 欧美一级特级 | 国产一级毛片高清视频 | 全免费午夜一级毛片真人 | lutube成人福利在线观看污 | 日韩视频在线观看免费视频 | 性 毛片| 请播放一级毛片 | 国产a级久久 | 欧美性生交zzzzzxxxxx | 国产午夜精品一区二区三区四区 | 视频一区二区三区免费观看 | 亚洲综合无码一区二区 | 爽爽淫人综合网网站 | www.99re1.com | 色妹子久久 | 欧美成人激情在线 | 在线观看视频亚洲 | 国产亚洲精品成人 | 轻点插视频 | 久久99精品久久久久久青青日本 | 日日噜噜噜夜夜狠狠久久蜜桃 | 国产一区二精品区在线 | 日日鲁一鲁视频 | 国产乱淫a∨片免费观看 | 九九热视频免费 | 国产一级免费视频 |