exception handling ( memory allocation)
Code:
#include <iostream>
#include <new>
using std::cout;
using std::endl;
using std::bad_alloc;
int main()
{
char* pdata = 0;
size_t count = ~static_cast<size_t>(0) / 3; // if you divide it by two it'll work
cout << endl
<< count
<< endl << count / 3 << endl;
try
{
pdata = new char[count];
cout << "Memory allocated." << endl;
}
catch(bad_alloc &ex)
{
cout << "Memory allocation failed." << endl
<< "The information from the exception object is: "
<< ex.what() << endl;
}
delete[] pdata;
return 0;
}
Look at the bold part of my code,
When I divide it by 3, the program doesn't responds, but when divide it by 2 the program works and throws an exception but not in the case of 3.
Why?