最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

簡單說說STL的內(nèi)存管理

 更新時(shí)間:2013年09月14日 09:25:15   作者:  
<STL 源碼剖析>將其描述為空間配置器,理由是allocator可以將其它存儲(chǔ)介質(zhì)(例如硬盤)做為stl 容器的存儲(chǔ)空間。由于內(nèi)存是allocator管理的主要部分,因此,本文以STL內(nèi)存管理為出發(fā)點(diǎn)介紹allocator

1. 概述
STL Allocator是STL的內(nèi)存管理器,也是最低調(diào)的部分之一,你可能使用了3年stl,但卻不知其為何物。

STL標(biāo)準(zhǔn)如下介紹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可以將其它存儲(chǔ)介質(zhì)(例如硬盤)做為stl 容器的存儲(chǔ)空間。由于內(nèi)存是allocator管理的主要部分,因此,本文以STL內(nèi)存管理為出發(fā)點(diǎn)介紹allocator。

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

本質(zhì)上,調(diào)用的是:

#include <vector>
std::vector<int, std::allocator> Array(100);
std::allocator就是一個(gè)簡單的Allocator

2. 使用
針對(duì)不同的應(yīng)用場合,STL中實(shí)現(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)境下,可以使用:

復(fù)制代碼 代碼如下:

#include <vector> 
#include <mt_allocator.h> 
std::vector<int, __gnu_cxx::__mt_alloc<int>> Array(100); 

3.一個(gè)簡單的Allocator實(shí)現(xiàn)
我們可以實(shí)現(xiàn)自己的allocator
復(fù)制代碼 代碼如下:

#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)存的分配與釋放可以實(shí)現(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); } 
};  

相關(guān)文章

  • C++實(shí)現(xiàn)秒表功能

    C++實(shí)現(xiàn)秒表功能

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)秒表功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • 盤點(diǎn)分析C語言中少見卻強(qiáng)大的字符串函數(shù)

    盤點(diǎn)分析C語言中少見卻強(qiáng)大的字符串函數(shù)

    這篇文章主要為大家盤點(diǎn)及分析C語言中少見卻強(qiáng)大的字符串函數(shù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-02-02
  • C++文件輸入輸出fstream使用方法

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

    C++標(biāo)準(zhǔn)庫提供了<fstream>頭文件,其中包含了用于文件輸入輸出的相關(guān)類和函數(shù),本文將詳細(xì)介紹<fstream>頭文件的使用方法,包括函數(shù)原型、打開文件、讀取和寫入文件、以及錯(cuò)誤處理等注意事項(xiàng),感興趣的朋友跟隨小編一起看看吧
    2023-10-10
  • C++中基本的輸入輸出函數(shù)使用指南

    C++中基本的輸入輸出函數(shù)使用指南

    這篇文章主要介紹了C++中基本的輸入輸出函數(shù)使用指南,是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-09-09
  • C++數(shù)據(jù)結(jié)構(gòu)與算法的基礎(chǔ)知識(shí)和經(jīng)典算法匯總

    C++數(shù)據(jù)結(jié)構(gòu)與算法的基礎(chǔ)知識(shí)和經(jīng)典算法匯總

    終是到了標(biāo)志著大二結(jié)束的期末考試了,對(duì)于《算法設(shè)計(jì)與分析》這門課,我需要總結(jié)一下學(xué)過的所有算法的思想以及老師補(bǔ)充的關(guān)于兩個(gè)復(fù)雜度和遞歸的概念思想,以及更深層次的理解,比如用畫圖的方式表達(dá)出來,我覺得可以用博客記錄總結(jié)一下,分享給大家,希望能有所幫助
    2022-05-05
  • C++?NFS掛載及掛載命令

    C++?NFS掛載及掛載命令

    這篇文章主要介紹了C++?NFS掛載,文中給大家提到了掛載NFS時(shí)常用的命令,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • Qt實(shí)現(xiàn)密碼顯示按鈕

    Qt實(shí)現(xiàn)密碼顯示按鈕

    這篇文章主要為大家詳細(xì)介紹了Qt實(shí)現(xiàn)密碼顯示按鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • 深入探討C++父類子類中虛函數(shù)的應(yīng)用

    深入探討C++父類子類中虛函數(shù)的應(yīng)用

    本篇文章是對(duì)C++父類子類中虛函數(shù)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • c++實(shí)現(xiàn)俄羅斯方塊游戲代碼

    c++實(shí)現(xiàn)俄羅斯方塊游戲代碼

    大家好,本篇文章主要講的是c++實(shí)現(xiàn)俄羅斯方塊游戲代碼,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • C++聯(lián)合體union用法實(shí)例詳解

    C++聯(lián)合體union用法實(shí)例詳解

    這篇文章主要介紹了C++聯(lián)合體union用法,較為詳細(xì)的分析了C++中聯(lián)合體的概念、實(shí)用技巧及相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2015-05-05

最新評(píng)論

长垣县| 内乡县| 玉环县| 潍坊市| 凌源市| 三都| 凉城县| 湘乡市| 遂川县| 海林市| 蕲春县| 维西| 信宜市| 东辽县| 泾川县| 邻水| 朔州市| 腾冲县| 辽中县| 东安县| 张北县| 永康市| 枝江市| 合江县| 二连浩特市| 横峰县| 平安县| 正宁县| 城口县| 双峰县| 道孚县| 石狮市| 鹰潭市| 赤峰市| SHOW| 石嘴山市| 呼伦贝尔市| 沅江市| 鲜城| 荆门市| 盘山县|