newexcpn.cpp 550 B

123456789101112131415161718192021222324252627282930
  1. // newexcp.cpp -- the bad_alloc exception
  2. #include <iostream>
  3. #include <new>
  4. #include <cstdlib> // for exit(), EXIT_FAILURE
  5. using namespace std;
  6. struct Big
  7. {
  8. double stuff[20000];
  9. };
  10. int main()
  11. {
  12. Big * pb;
  13. pb = new (std::nothrow) Big[10000]; // 1,600,000,000 bytes
  14. if (pb == NULL)
  15. {
  16. cout << "Could not allocate memory. Bye.\n";
  17. cin.get();
  18. exit(EXIT_FAILURE);
  19. }
  20. cout << "Memory successfully allocated\n";
  21. pb[0].stuff[0] = 4;
  22. cout << pb[0].stuff[0] << endl;
  23. delete [] pb;
  24. // cin.get();
  25. return 0;
  26. }