I'm learning C++ at the moment and in my textbook there is the following example:
Now, what should happen according to the book is, that if I try to reserve more space than RAM and Swap space together, it should throw that exception. However, when I try to allocate more memory than available, the following message apears:
In case it makes any difference, I'm using gcc 4.2.4 on a Linux system and have 3GB of memory available (RAM + Swap).
I am guessing, that this example is either wrong in general or other Compilers make it react differently. Could someone here enlighten me?
Greetings,
Nepomuk
Code:
#include <iostream>
using namespace std;
int main() {
char *puffer;
int mb;
try {
cout << "How many MB should be reserved? ";
cin >> mb;
puffer = new char[256 * 4 * 1024 * mb];
if(puffer == NULL)
throw "Error with memory allocation!";
}
catch (char * str) {
cout << "Exception: " << str << endl;
return 1;
}
cout << "Success!" << endl;
return 0;
}
Code:
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Aborted
I am guessing, that this example is either wrong in general or other Compilers make it react differently. Could someone here enlighten me?
Greetings,
Nepomuk
Comment