国产精品成人一区二区在线_日本淫妇xxww老女人,_黑人让我高潮的视频_欧美亚洲高清在线一区_国产丝袜久久久久之久_国产精品这里有精品_亚洲aⅴ男人的天堂t在线观看_免费黄色片一级毛片

簡單說說何為之STL的內(nèi)存管理Allocator

2021-06-12 說說

  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.

  將其描述為空間配置器,理由是allocator可以將其它存儲(chǔ)介質(zhì)(例如硬盤)做為stl 容器的存儲(chǔ)空間。由于內(nèi)存是allocator管理的主要部分,因此,本文以STL內(nèi)存管理為出發(fā)點(diǎn)介紹allocator。

  Allocator就在我們身邊,通常使用STL的方式:

  #include

  std::vectorArray(100);

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

  #include

  std::vectorArray(100);

  std::allocator就是一個(gè)簡單的Allocator

  2. 使用

  針對(duì)不同的應(yīng)用場(chǎ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_allocatorSimply wraps ::operator new and ::operator .

  __gnu_cxx::malloc_allocatorSimply wraps malloc and free. There is also a hook for an out-of-memory handler

  __gnu_cxx::debug_allocatorA 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_allocA high-performance, single pool allocator. The reusable memory is shared among identical instantiations of this type.

  __gnu_cxx::__mt_allocA 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

  #include

  std::vectorArray(100);

  3.一個(gè)簡單的Allocator實(shí)現(xiàn)

  我們可以實(shí)現(xiàn)自己的allocator

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

  #include

  template

  class my_allocator : public std::allocator

  {

  public:

  typedef std::allocatorbase_type;

  // 必須要重新定義

  template

  struct rebind

  {

  typedef my_allocatorother;

  };

  // 內(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_allocatorconst&)

  {}

  my_allocator& operator=(my_allocatorconst&)

  {

  return (*this);

  }

  template

  my_allocator(my_allocatorconst&)

  {}

  template

  my_allocator& operator=(my_allocatorconst&)

  {

  return (*this); }

  };

【簡單說說何為之STL的內(nèi)存管理Allocator】相關(guān)文章:

有關(guān)STL應(yīng)用論文06-14

簡單說說學(xué)校機(jī)房的管理05-04

人生何為苦何為樂何為幸福02-25

經(jīng)典美文:人生的舞臺(tái)該如何為之演繹03-15

何為幸福何為夫妻何為家雜文隨筆07-04

何為文明,何為美德?作文08-02

何為情,何為義的散文07-27

何為飽而知人之饑文言文05-12

何為貧?何為富?750字08-25

和顺县| 无锡市| 汉寿县| 青铜峡市| 新河县| 江门市| 车险| 明溪县| 华亭县| 绥芬河市| 金川县| 石林| 星子县| 津市市| 丹东市| 东阿县| 仪陇县| 香港 | 景谷| 象州县| 格尔木市| 岢岚县| 邳州市| 广水市| 常宁市| 金阳县| 株洲县| 资中县| 达州市| 台山市| 仙居县| 交城县| 喀喇沁旗| 霍林郭勒市| 东辽县| 山西省| 裕民县| 修文县| 南靖县| 宁晋县| 永德县|