Export (0) Print
Expand All

uninitialized_fill_n

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

template<class FwdIt, class Size, class Type> 
   void uninitialized_fill_n( 
      ForwardIterator _First,  
      Size _Count, 
      const Type& _Val 
   );

_First

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

_Count

The number of elements to be initialized.

_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 ( 0 < count-- )
   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_n.cpp
// compile with: /EHsc /W3
#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 ( 60 );
   Integer* Array = ( Integer* ) malloc( N * sizeof( int ) );
   uninitialized_fill_n( Array, N, val );  // C4996
   int i;
   cout << "The uninitialized Array contains: ";
   for ( i = 0 ; i < N; i++ )
      cout << Array [ i ].get( ) <<  " ";
}

The uninitialized Array contains: 60 60 60 60 60 60 60 60 60 60 

Header: <memory>

Namespace: std

Show:
© 2014 Microsoft