Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


Allocates a block of memory large enough to store at least some specified number of elements.

template<class Other>
   pointer allocate(
      size_type _Count, 
      const Other* _Hint = 0



The number of elements for which sufficient storage is to be allocated.


A const pointer that may assist the allocator object satisfy the request for storage by locating the address of an object allocated prior to the request.

A pointer to the allocated object.

The member function allocates storage for an array of count elements of type Type, by calling operator new(_Count). It returns a pointer to the allocated object. The hint argument helps some allocators in improving locality of reference; a valid choice is the address of an object earlier allocated by the same allocator object and not yet deallocated. To supply no hint, use a null pointer argument instead.

// allocator_allocate.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>

using namespace std;

int main( ) 
   allocator<int> v1Alloc;

   allocator<int>::pointer v1aPtr;

   v1aPtr = v1Alloc.allocate ( 10 );

   int i;
   for ( i = 0 ; i < 10 ; i++ )
      v1aPtr[ i ] = i;

   for ( i = 0 ; i < 10 ; i++ )
      cout << v1aPtr[ i ] << " ";
   cout << endl;

   v1Alloc.deallocate( v1aPtr, 10 );


0 1 2 3 4 5 6 7 8 9 

Header: <memory>

© 2015 Microsoft