Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

allocator::allocate 

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
   );

Parameters

_Count

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

_Hint

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 );
}

Output

0 1 2 3 4 5 6 7 8 9 

Header: <memory>

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.