Object-Oriented Programming in C++

Code display of PL10/badalloc.cpp

// PL10/badalloc.cpp
// illustrates an exception thrown by new (allocating memory)
// Niels Walet. Last edited 03/12/2019
#include<iostream>
#include<memory>
int main() {
  double *my_array;
  try
    {
      my_array = new double[10000000000000000];
    }
  catch(std::bad_alloc memFail)
    {
      std::cerr<<"Memory allocation failure"<<std::endl;
      return(1);
    }
  delete[] my_array;
  return 0;
}
Download here; To copy and paste: double click inside code, then copy.