Handling Insufficient Memory Conditions
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

Handling Insufficient Memory Conditions

Testing for failed memory allocation can be done with code such as the following:

// insufficient_memory_conditions.cpp

#include <iostream>

using namespace std;

#define BIG_NUMBER 100000000

int main()


   int *pI = new int[BIG_NUMBER];

   if( pI == 0x0 )


      cout << "Insufficient memory" << endl;

      return -1;


}There is another ways to handle failed memory allocation requests: write a custom recovery routine to handle such a failure, then register your function by calling the _set_new_handler run-time function.

See Also

The operator new Function

© 2016 Microsoft