By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,104 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,104 IT Pros & Developers. It's quick & easy.

bad_alloc unhandled exception

P: n/a
Hi
Shouldn't this code cause a core dump when the sys runs out of mem,
when there's no default exception handling mechanism.

void fn()
{
SomeObj *so = NULL;
so = new SomeObj; // -> no mem? throws bad_alloc execption
// but if no one's catching it, then core
//dump?
if (so == NULL)
{ // perform exception handling

}
//etc...

}
Thanks!
Jon

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> Shouldn't this code cause a core dump when the sys runs out of mem,
when there's no default exception handling mechanism.
When an exception is thrown be never catched, terminate() is called
which must never return (meaning abort the program). Whether it causes
a core dump, brings you in the debugger or whatever is implementation
defined.
void fn()
{
SomeObj *so = NULL;
so = new SomeObj; // -> no mem? throws bad_alloc execption
// but if no one's catching it, then core
//dump?
if (so == NULL)
This will never be true.
{ // perform exception handling

}
//etc...

}


Note that you may change the default behavior by calling
set_terminate().

Jonathan

Jul 23 '05 #2

P: n/a
>>so = new SomeObj; // -> no mem? throws bad_alloc execption
// but if no one's catching it, then core
//dump?
if (so == NULL)
{ // perform exception handling

}
//etc...

exception handling is performed through try/catch block. new may never
return null, on failing.
try{
so = new SomeObj;
}
catch(bad_alloc &e)
{
////perform exception handling
}

Jul 23 '05 #3

P: n/a
Thanks ! in fact simulated a bad_alloc using setrlimit() to limit the
heap size for the proc... terminates()...

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.