hey all, someone just asked me why their program continues to allocate when the exception has been caught. the exception being a failed allocation. here is the code:
now I'm not a C++ programmer, but I just can't explain this output. here is the example output from this program:Code:#include <iostream> using std::cin; using std::cout; using std::endl; using std::cerr; #include <iomanip> using std::setbase; #include <new> using std::bad_alloc; int main() { double *ptr[1000]; try { for (int x = 0; x<1000; x++) { ptr[x] = new double [5000000]; cout << "allocated 5000000 doubles in " << x << endl; } } catch (bad_alloc & memoryAllocationException ) { cerr <<"Exception occured: "<< memoryAllocationException.what() << endl; } return 0; }
why does it continue to allocate memory which technically shouldn't be available? could this be a buffering problem, because of the fact that the exception message is passing through stderr and not stdout (it is getting printed before the messages in stdout are processed/outputted)?allocated 5000000 doubles in 37
allocated 5000000 doubles in 38
allocated 5000000 doubles in 39
allocated 5000000 doubles in 40
allocated 5000000 doubles in 41
Exception occured: bad alloc exception thrown
allocated 5000000 doubles in 42
allocated 5000000 doubles in 43
allocated 5000000 doubles in 44
allocated 5000000 doubles in 45
allocated 5000000 doubles in 46
allocated 5000000 doubles in 47
allocated 5000000 doubles in 48
allocated 5000000 doubles in 49
allocated 5000000 doubles in 50
could anyone here please offer me some insight as to why such output might be occuring?
thank you in advance.