Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++中的分配器是如何实现的? 我试图理解一个更好的STD::C++中的分配器,我发现,实际上,只有像STD::vector容器通常只使用一个分配器类:我的问题是如何实现这样的分配器?它是否像一个定期重新分配的堆栈?如果不是,它实际上是如何实现的?_C++_Memory Management - Fatal编程技术网

C++中的分配器是如何实现的? 我试图理解一个更好的STD::C++中的分配器,我发现,实际上,只有像STD::vector容器通常只使用一个分配器类:我的问题是如何实现这样的分配器?它是否像一个定期重新分配的堆栈?如果不是,它实际上是如何实现的?

C++中的分配器是如何实现的? 我试图理解一个更好的STD::C++中的分配器,我发现,实际上,只有像STD::vector容器通常只使用一个分配器类:我的问题是如何实现这样的分配器?它是否像一个定期重新分配的堆栈?如果不是,它实际上是如何实现的?,c++,memory-management,C++,Memory Management,默认的分配器是,在需要时只使用::operator new,所以没有什么特别的。这与为每个需要的对象进行“新建”和“删除”差不多。您可以在标准中的[default.allocator]下阅读更多关于它的信息 分配器接口实际上只是一组在模板实例化期间强制执行的需求,是这个过程的包装器,允许使用其他内存供应方法 例如,您可能提供的替代分配器可以实现一个或其他特定于您的需求的分配器,从而减少诚实的动态分配 标准容器及其元素类型都有一个分配器类型作为模板参数,您通常不会注意到这一点!这就是您选择用于该容

默认的分配器是,在需要时只使用::operator new,所以没有什么特别的。这与为每个需要的对象进行“新建”和“删除”差不多。您可以在标准中的[default.allocator]下阅读更多关于它的信息

分配器接口实际上只是一组在模板实例化期间强制执行的需求,是这个过程的包装器,允许使用其他内存供应方法

例如,您可能提供的替代分配器可以实现一个或其他特定于您的需求的分配器,从而减少诚实的动态分配

标准容器及其元素类型都有一个分配器类型作为模板参数,您通常不会注意到这一点!这就是您选择用于该容器的替代实现的方式

在这些情况下,您通常会预先分配一些大内存块,然后在适当的时候抛出一些小内存块。从这个意义上说,这样的实现可以被视为堆中的一种堆,但实际上根本没有理由给它堆语义。它只需要遵守概念的要求

约瑟蒂斯先生在华盛顿举了一个无聊的例子;我在这里复制它:

/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <limits>
#include <iostream>

namespace MyLib {
   template <class T>
   class MyAlloc {
     public:
       // type definitions
       typedef T        value_type;
       typedef T*       pointer;
       typedef const T* const_pointer;
       typedef T&       reference;
       typedef const T& const_reference;
       typedef std::size_t    size_type;
       typedef std::ptrdiff_t difference_type;

       // rebind allocator to type U
       template <class U>
       struct rebind {
           typedef MyAlloc<U> other;
       };

       // return address of values
       pointer address (reference value) const {
           return &value;
       }
       const_pointer address (const_reference value) const {
           return &value;
       }

       /* constructors and destructor
        * - nothing to do because the allocator has no state
        */
       MyAlloc() throw() {
       }
       MyAlloc(const MyAlloc&) throw() {
       }
       template <class U>
         MyAlloc (const MyAlloc<U>&) throw() {
       }
       ~MyAlloc() throw() {
       }

       // return maximum number of elements that can be allocated
       size_type max_size () const throw() {
           return std::numeric_limits<std::size_t>::max() / sizeof(T);
       }

       // allocate but don't initialize num elements of type T
       pointer allocate (size_type num, const void* = 0) {
           // print message and allocate memory with global new
           std::cerr << "allocate " << num << " element(s)"
                     << " of size " << sizeof(T) << std::endl;
           pointer ret = (pointer)(::operator new(num*sizeof(T)));
           std::cerr << " allocated at: " << (void*)ret << std::endl;
           return ret;
       }

       // initialize elements of allocated storage p with value value
       void construct (pointer p, const T& value) {
           // initialize memory with placement new
           new((void*)p)T(value);
       }

       // destroy elements of initialized storage p
       void destroy (pointer p) {
           // destroy objects by calling their destructor
           p->~T();
       }

       // deallocate storage p of deleted elements
       void deallocate (pointer p, size_type num) {
           // print message and deallocate memory with global delete
           std::cerr << "deallocate " << num << " element(s)"
                     << " of size " << sizeof(T)
                     << " at: " << (void*)p << std::endl;
           ::operator delete((void*)p);
       }
   };

   // return that all specializations of this allocator are interchangeable
   template <class T1, class T2>
   bool operator== (const MyAlloc<T1>&,
                    const MyAlloc<T2>&) throw() {
       return true;
   }
   template <class T1, class T2>
   bool operator!= (const MyAlloc<T1>&,
                    const MyAlloc<T2>&) throw() {
       return false;
   }
}
使用方法:

#include <vector>
#include "myalloc.hpp"

int main()
{
    // create a vector, using MyAlloc<> as allocator
    std::vector<int,MyLib::MyAlloc<int> > v;

    // insert elements
    // - causes reallocations
    v.push_back(42);
    v.push_back(56);
    v.push_back(11);
    v.push_back(22);
    v.push_back(33);
    v.push_back(44);
}

分配器只提供分配内存、释放内存、构造对象和销毁它们的策略

它们不提供增加先前分配的内存区域大小来重新分配内存的策略。因此,在所有容器中,如果必须增加内存:

容器分配一个新的内存块, 或者像vector那样,分配一个新的更大的内存区域,复制其中的旧元素,然后释放以前的内存区域。
这个答案似乎更多的是关于容器如何请求内存,而不是分配器如何提供内存。@LightnessRacesinOrbit所以我在回答这个问题,而你没有回答??我的观点正好相反,但也许那只是我自己!但是容器和分配器是如何交互的呢?假设我有一个整数列表,我想插入一个新的整数值,天真地我会使用new来分配一个新节点并适当地链接它。分配器会有什么不同?默认分配器不会有任何不同。您自己的替代方案可以返回指向预分配存储中某个位置的指针,或者返回指向存储在鸽子翅膀上的某个内存的指针。有没有实际的示例,其中没有实际使用默认分配器?如果您想确切地了解它是如何实现的,请查看一下stdlib的源代码,但老实说,这不会很有趣。分配器有一些遵循的函数,给我一些内存,收回一些内存,这是它的标准容器,默认情况下使用默认分配器,它实际上只是new&delete的包装器。但这是一个模板参数,所以你可以让他们使用你想要的任何东西。我添加了一个进一步的链接。