PracticeDev/study_cpp/cpp_primer_source_code/Chapter 15/newexcpn.cpp

31 lines
580 B
C++
Raw Normal View History

2022-12-20 17:31:11 +08:00
// newexcp.cpp -- the bad_alloc exception
#include <iostream>
#include <new>
#include <cstdlib> // for exit(), EXIT_FAILURE
using namespace std;
struct Big
{
double stuff[20000];
};
int main()
{
Big * pb;
pb = new (std::nothrow) Big[10000]; // 1,600,000,000 bytes
if (pb == NULL)
{
cout << "Could not allocate memory. Bye.\n";
cin.get();
exit(EXIT_FAILURE);
}
cout << "Memory successfully allocated\n";
pb[0].stuff[0] = 4;
cout << pb[0].stuff[0] << endl;
delete [] pb;
// cin.get();
return 0;
}