理解allocator

參考:http://oss.org.cn/?action-viewnews-itemid-3744

allocator 是一個類,有着叫allocate()deallocate()成員函數(相當於mallocfree)。它還有用於維護所分配的內存的輔助函數和指示如何使用這些內存的typedef(指針或引用類型的名字)。

舉個例子vector::get_allocator(代碼來自http://www.cplusplus.com/reference/stl/vector/get_allocator/)

// vector::get_allocator
#include <iostream>
#include <vector>

using namespace std;

int main ()
{
  vector<int> myvector;
  int * p;
  unsigned int i;

  // allocate an array of 5 elements using vector's allocator:
  p=myvector.get_allocator().allocate(5);

  // assign some values to array
  for (i=0; i<5; i++) p[i]=i;

  cout << "The allocated array contains:";
  for (i=0; i<5; i++) cout << " " << p[i];
  cout << endl;

  myvector.get_allocator().deallocate(p,5);

  return 0;
}
Returns the allocator object used to construct the vector.

allocator也參考(You can see an example of a pool allocator in the open source SGIPro64TM?compiler):http://oss.sgi.com/projects/Pro64/

也參考:http://www.cplusplus.com/reference/std/memory/allocator/

Default allocator
Allocators are classes that define memory models to be used by some parts of the Standard Library, and most specifically, by STL containers.

This section describes the default allocator template allocator (lowercase). This is the allocator that all standard containers will use if their last (and optional) template parameter is not specified, and is the only predefined allocator in the standard library.

Other allocators may be defined. Any class having the same members as this default allocator and following its minimum requirements can be used as an allocator -- notice that only under very specific circumstances this is needed.

Technically, a memory model described by allocators might be specialized for each type of object to be allocated and even may store local data for each container they work with. Although this does not happen with the defaultallocator.

類模板allocator的定義如下:

template < class T > class allocator;

Taking one template parameter (which is assumed to be T in this entire reference).

其成員變量和成員函數如下:

Member types

member definition in allocator represents
value_type T Element type
pointer T* Pointer to element
reference T& Reference to element
const_pointer const T* Constant pointer to element
const_reference const T& Constant reference to element
size_type size_t Quantities of elements
difference_type ptrdiff_t Difference between two pointers

Member functions

同時也可以看看MSDN中的定義:

template<class T>
    class allocator {
    typedef size_t size_type;
    typedef ptrdiff_t difference_type;
    typedef T *pointer;
    typedef const T *const_pointer;
    typedef T& reference;
    typedef const T& const_reference;
    typedef T value_type;
    pointer address(reference x) const;
    const_pointer address(const_reference x) const;
    allocator();
    allocator<T>& operator=(const allocator<T>);
    pointer allocate(size_type n, const void *hint);
    void deallocate(pointer p, size_type n);
    void construct(pointer p, const T& val);
    void destroy(pointer p);
    size_type max_size() const;
    };




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章