catch: out of memory in c++, process killed in lunux, any solution?
-
Hello guru developers, I have a project which i have to use a lot of memory allocation, the project works fine except when there are a lot of data the process is killed by Linux, i don't know why? For example this example: it should exit when there is no free memory, but it is killed after some time when i run this program, Does anyone has any idea how to solve this problem in Linux? thanks in advance
#include
#include
#include
#include
#include
#includeusing namespace std;
struct tree_node
{
list<tree_node*> leaf;
char data;
short IsEndPoint;
};int main()
{try { for (int i = 0 ; i < 9999000; i++ ) { tree\_node\* t = new tree\_node\[1000\]; printf("id: %d \\r ", i); }
}
catch(...)
{
cout<<"test"<< endl;
exit(0);
}cin.get();
return 0;
}It is never late to learn
-
Hello guru developers, I have a project which i have to use a lot of memory allocation, the project works fine except when there are a lot of data the process is killed by Linux, i don't know why? For example this example: it should exit when there is no free memory, but it is killed after some time when i run this program, Does anyone has any idea how to solve this problem in Linux? thanks in advance
#include
#include
#include
#include
#include
#includeusing namespace std;
struct tree_node
{
list<tree_node*> leaf;
char data;
short IsEndPoint;
};int main()
{try { for (int i = 0 ; i < 9999000; i++ ) { tree\_node\* t = new tree\_node\[1000\]; printf("id: %d \\r ", i); }
}
catch(...)
{
cout<<"test"<< endl;
exit(0);
}cin.get();
return 0;
}It is never late to learn
I never programmed with gcc in linux so I might be wrong. I am guess that new operator does not throw an exception but terminated the program when cannot allocate memory. -Saurabh
-
I never programmed with gcc in linux so I might be wrong. I am guess that new operator does not throw an exception but terminated the program when cannot allocate memory. -Saurabh
how could you catch exception memory is full? is there any way in c++ for windows? not for linux, it must be same in linux
It is never late to learn
-
how could you catch exception memory is full? is there any way in c++ for windows? not for linux, it must be same in linux
It is never late to learn
Well you already have the code to catch the exception...using a catch block. When new operator cannot allocate memory then it throws std::bad_alloc exception. Also the value of returned pointer is NULL. -Saurabh
-
Well you already have the code to catch the exception...using a catch block. When new operator cannot allocate memory then it throws std::bad_alloc exception. Also the value of returned pointer is NULL. -Saurabh
Thanks for reply, I run this code under Visual c++ 2005, it does not throw any exception, it crashes computer, do u know why it crashes computer? Thanks
It is never late to learn
-
Thanks for reply, I run this code under Visual c++ 2005, it does not throw any exception, it crashes computer, do u know why it crashes computer? Thanks
It is never late to learn
If my calculation is correct then you are requesting about 260.74GB of memory!!! Which of course cannot be allocated. I don't think that your program should crash, instead you should get nice error from windows stating Windows is running low on memory so termination your program. Just out of curiosity what are you trying to do? -Saurabh
-
If my calculation is correct then you are requesting about 260.74GB of memory!!! Which of course cannot be allocated. I don't think that your program should crash, instead you should get nice error from windows stating Windows is running low on memory so termination your program. Just out of curiosity what are you trying to do? -Saurabh
I took such a large memory on purpose to test bad allocation exception, i figure out how to do that,here is my final code, this works,
# include # include # include #include struct tree_node
{
std::list<tree_node*> leaf;
char data;
short IsEndPoint;
};void MyHandler()
{
std::cout << "MyHandler Called... " << std::endl;
throw std::bad_alloc();
}int main()
{
std::set_new_handler( MyHandler );char\* ch; try { for( unsigned long Idx( 0 ); Idx < 0xffffffff; ++Idx ) { new tree\_node\[10000\]; printf("id: %d \\r",Idx); } } catch( const std::bad\_alloc& e ) { std::cout << e.what() << std::endl; } std::cin.get(); return 0;
}
It is never late to learn