Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
uninitialized_fill

uninitialized_fill

Copies objects of a specified value into an uninitialized destination range.

template<class ForwardIterator, class Type> 
   void uninitialized_fill( 
      ForwardIterator _First,  
      ForwardIterator _Last, 
      const Type& _Val 
   );

_First

A forward iterator addressing the first element in the destination range that is to be initiated.

_Last

A forward iterator addressing the last element in the destination range that is to be initiated.

_Val

The value to be used to initialize the destination range.

This algorithm allows the decoupling of memory allocation from object construction.

The template function effectively executes:

while ( _First!= _Last )
   new ( (void *)&*_First ++)
      iterator_traits<ForwardIterator>::value_type ( _Val );

unless the code throws an exception. In that case, all constructed objects are destroyed and the exception is rethrown.

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

using namespace std;

   class Integer {         // No default constructor
   public:
      Integer( int x ) : val( x ) {}
      int get( ) { return val; }
   private:
      int val;
   };

int main( )
{
   const int N = 10;
   Integer val ( 25 );
   Integer* Array = ( Integer* ) malloc( N * sizeof( int ) );
   uninitialized_fill( Array, Array + N, val );
   int i;
   cout << "The initialized Array contains: ";
      for ( i = 0 ; i < N; i++ )
      {
         cout << Array [ i ].get( ) << " ";
      }
   cout << endl;
}
The initialized Array contains: 25 25 25 25 25 25 25 25 25 25 

Header: <memory>

Namespace: std

Show:
© 2015 Microsoft