代码改变世界

c++ allocator类

2012-06-07 13:25  youxin  阅读(1099)  评论(0编辑  收藏  举报

   <memory>头文件提供了一个类allocator<T>,它可以分配用来保存T类型的对象的整块内存,而不需要初始化,并且他会返回一个指针,指向这块内存的首元素。这样的指针是危险的,因为他们的类型表面指向的是对象,但是这些内存却并不包含真正的对象。标准库还提供了一种方式在这些内存中构造对象,销毁对象--但是并没有释放内存。由程序员使用allocator类来确定哪些空间保存构造的对象,哪些空间还未初始化。

  对我们的目的来说,allocator类中最有趣的部分是,它包含4个成员函数和两个相关的 非成员函数(不仅仅只有4个,有多个重载版本)

<template class T>class allocator{

public :

T* allocate(size_t);

void deallocate(T*,size_t);

void  construct(T*,const T&);

void destroy(T*);

//...

};

 

pointer allocate (size_type n, allocator<void>::const_pointer hint=0);                                                                                                                

     Allocate block of storage

void deallocate (pointer p, size_type n); 释放为初始化的内存

      Releases a block of storage previously allocated with member allocate and not yet released

 

 void construct ( pointer p, const_reference val );                                                                                                                                     

   Construct an object

   Constructs an object of type T (the template parameter) on the location pointed by p using its copy constructor to initialize its value to val.

Notice that this does not allocate space for the element, it should already be available at p (see member allocate to allocate space).

 It is equivalent to:

     new ((void*)p) T (val);

 
void destroy (pointer p);                                                                        
  Destroy an object
  Destroys the object of type T (the template parameter) pointed by p.

Notice that this does not deallocate space for the element. (see member deallocate to release storage space).

It is equivalent to:  ((T*)p)->~T();
 
address 

  obtains the address of an object, even if operator& is overloaded
 (public member function)

rebind: 一个嵌套(nested)的class template,class rebind<U>拥有唯一成员other,那是一个typedef,代表allocator<U>

参考:http://en.cppreference.com/w/cpp/memory/allocator

Notes

The member template class rebind provides a way to obtain an allocator for a different type. For example,

std::list<T, A> allocates nodes of some internal type Node<T>, using the allocator A::rebind<Node<T>>::other (until C++11)
std::list<T, A> allocates nodes of some internal type Node<T>, using the allocator std::allocator_traits<A>::rebind_alloc<Node<T>>, which is implemented in terms of A::rebind<Node<T>>::other if A is an std::allocator (since C++11)

 

 与allocator类相关的两个非成员函数是:
template<class In,class For> For uninitialized_copy(In,In,For);
   作用于copy类似,把前两个参数指定的序列中的值,负责到第三个参数的目的序列中。
 
template<class For,class T> vooid uniinitialized_fill(For,For,const T&) ; 使用指定值来填充未初始化的空间。
 

列表:allocator类分配内存以及构建对象时候,常用的几个方法:

allocator<T> a;

Defines an allocator object named a that can allocate memory or construct objects of type T.

a.allocate(n)

Allocates raw, unconstructed memory to hold n objects of type T.

a.deallocate(p, n)

Deallocates memory that held n objects of type T starting at address contained in the T* pointer named p. It is the user's responsibility to run destroy on any objects that were constructed in this memory before calling deallocate.

a.construct(p, t)

Constructs a new element in the memory pointed to by the T* pointer p. The copy constructor of type T is run to initialize the object from t.

a.destroy(p)

Runs the destructor on the object pointed to by the T* pointer p.

 uninitialized_copy(b, e, b2)

Copies elements from the input range denoted by iterators b and e into unconstructed, raw memory beginning at iterator b2. The function constructs elements in the destination, rather than assigning them. The destination denoted by b2 is assumed large enough to hold a copy of the elements in the input range.

 uninitialized_fill(b, e, t)

Initializes objects in the range denoted by iterators b and e as a copy oft. The range is assumed to be unconstructed, raw memory. The objects are constructed using the copy constructor.

 uninitialized_fill_n(b, e, t, n)

Initializes at most an integral number n objects in the range denoted by iterators b and e as a copy of t. The range is assumed to be at least nelements in size. The objects are constructed using the copy constructor.

 更多:

http://mathbox59.blog.163.com/blog/static/12845359920102325635884/

http://www.yekezhong.com/574

http://cissco.iteye.com/blog/379093

http://www.codeproject.com/Articles/4795/C-Standard-Allocator-An-Introduction-and-Implement

 http://en.cppreference.com/w/cpp/memory/allocator