Hi, I'm writing program that performs some calculations. User needs to enter a number of coeficients, and then program will calculate coeficients according some algorithm.
Basically I have this problem:
User can enter enormous number of wanted coefficients and allocation may fail.When I compile this code I get warning:Code:#include<new> #include<iostream> using namespace std; int main() { int* arr; try { arr=new int[0x7fffffff]; } catch(bad_alloc &ba) { cout<<ba.what(); } catch(...) { cout<<"Something went wrong!"; } }
warning C4307: '*' : integral constant overflow.
And this simply doesn't work. I get exception in dbgheap.c
Actually my program is much more complicated (it is dialog based MFC application) but I decided to go step by step.
By the way I found this code in MSDN and it works:
If someone can explain me why?Code:#include<new> #include<iostream> using namespace std; class MyClass { public: MyClass( ) { }; ~MyClass( ) { }; int imember[30]; }; int main( ) { MyClass* fPtr; try { fPtr = new MyClass[0x7fffffff]; delete[ ] fPtr; } catch( bad_alloc &ba) { cout << ba.what( ) << endl; }; }



LinkBack URL
About LinkBacks



CornedBee