From: Palmer E. <the...@gm...> - 2012-12-13 14:24:42
|
Copying from satck overflow : http://stackoverflow.com/questions/13851244/cant-catch-bad-alloc-in-mingw See comments there - people suggest it is a mingw bug (the title was edited not by me) COPY ----------------> I am writing a program for a homework where I need to allocate many objects to check things on locality and performance etc. I can't seem to catch the exception thrown by `new` #include "List.h" #include<iostream> #include <exception> int main(int argc, char **argv) { cout << "size of List c++ : " << sizeof(List) << endl; //16 List * ptrList = new List(); unsigned long var = 0; try { for (;; ++var) { List * ptrList2 = new List(); ptrList->next = ptrList2; ptrList2->previous = ptrList; ptrList = ptrList2; } } catch (bad_alloc const& e) { cout << "caught : " << e.what() << endl; // } catch (...) { //this won't work either } Results in : > This application has requested the Runtime to terminate it in an > unusual way. Please contact the application's support team for more > information. If I change the allocation part as : List * ptrList2 = new (nothrow) List(); if (!ptrList2) { cout << "out of memory - created " << var << " nodes" << endl; break; } I get a nice : out of memory - created 87921929 nodes Why can't I catch `bad_alloc` ? I am on mingwin on Windows 7 x64 Pro C:\Users\MrD>g++ --version g++ (GCC) 4.7.2 The List : class List { long j; public: List * next; List * previous; virtual long jj() { return this->j; } List() { next = previous = 0; j = 0; } virtual ~List() { if (next) { next->previous = this->previous; } if (previous) { previous->next = this->next; } } }; -- View this message in context: http://mingw-users.1079350.n2.nabble.com/unable-to-catch-bad-alloc-tp7579725.html Sent from the MinGW-users mailing list archive at Nabble.com. |