Namespaces
Variants
Views
Actions

std::allocator

From cppreference.com
< cpp‎ | memory
Revision as of 20:43, 15 February 2013 by 109.80.56.5 (Talk)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <memory>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td >
template< class T >
struct allocator;
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
template<>
struct allocator<void>;
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

The std::allocator class template is the default Allocator used by all standard library containers if no user-specified allocator is provided. The default allocator is stateless, that is, all instances of the given allocator are interchangeable, compare equal and can deallocate memory allocated by any other instance of the same allocator type.

Specialization for void lacks the member typedefs reference, const_reference, size_type and difference_type. This specialization declares no member functions.

Template:rev list begin Template:rev list item Template:rev list item Template:rev list end

Contents

Member types

Type Definition
value_type T
pointer T*
const_pointer const T*
reference T&
const_reference const T&
size_type std::size_t
difference_type std::ptrdiff_t
rebind template< class U > struct rebind { typedef allocator<U> other; };

Member functions

Template:cpp/memory/allocator/dcl list allocatorTemplate:cpp/memory/allocator/dcl list ~allocatorTemplate:cpp/memory/allocator/dcl list addressTemplate:cpp/memory/allocator/dcl list allocateTemplate:cpp/memory/allocator/dcl list deallocateTemplate:cpp/memory/allocator/dcl list max sizeTemplate:cpp/memory/allocator/dcl list constructTemplate:cpp/memory/allocator/dcl list destroy

Non-member functions

Template:cpp/memory/allocator/dcl list operator cmp

Notes

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

Template:rev list begin Template:rev list item Template:rev list item Template:rev list end

Example

#include <memory>
#include <iostream>
#include <string>
 
int main()
{
    std::allocator<int> a1; // default allocator for ints
    int* a = a1.allocate(10); // space for 10 ints
 
    a[9] = 7;
 
    std::cout << a[9] << '\n';
 
    a1.deallocate(a, 10);
 
    // default allocator for strings
//    std::allocator<std::string> a2;
 
    // same, but obtained by rebinding from the type of a1
//    decltype(a1)::rebind<std::string>::other a2;
 
    // same, but obtained by rebinding from the type of a1 via allocator_traits
    std::allocator_traits<decltype(a1)>::rebind_alloc<std::string> a2;
 
    std::string* s = a2.allocate(2); // space for 2 string
 
    a2.construct(s, "foo");
    a2.construct(s + 1, "bar");
 
    std::cout << s[0] << ' ' << s[1] << '\n';
 
    a2.destroy(s);
    a2.destroy(s + 1);
    a2.deallocate(s, 2);
}

Output:

7
foo bar

See also

Template:cpp/memory/dcl list allocator traitsTemplate:cpp/memory/dcl list scoped allocator adaptorTemplate:cpp/memory/dcl list uses allocator