First page Back Continue Last page Graphics

fig13_04.cpp (1 of 2)

  • 1 // Fig. 13.4: fig13_04.cpp
  • 2 // Demonstrating pre-standard new returning 0 when memory
  • 3 // is not allocated.
  • 4 #include <iostream>
  • 5
  • 6 using std::cout;
  • 7
  • 8 int main()
  • 9 {
  • 10 double *ptr[ 50 ];
  • 11
  • 12 // allocate memory for ptr
  • 13 for ( int i = 0; i < 50; i++ ) {
  • 14 ptr[ i ] = new double[ 5000000 ];
  • 15
  • 16 // new returns 0 on failure to allocate memory
  • 17 if ( ptr[ i ] == 0 ) {
  • 18 cout << "Memory allocation failed for ptr[ "
  • 19 << i << " ]\n";
  • 20
  • 21 break;
  • 22
  • 23 } // end if
  • 24