On Thu, Mar 06, 2003 at 06:25:32AM -0600, David D. Hagood wrote:
> Linus Torvalds wrote:
> 
> >Also note that if you don't allow exceptions (which I would _strongly_ 
> >encourage), you can't really use "new" - unless you think it's ok to 
> >SIGSEGV under low-mem circumstances. Which it might be, of course, in some 
> >situations.
> >
> 
> I do embedded C++ using GCC for a living - operator new() will return a 
> NULL if the object cannot be created, unless you use set_new_hander() to 
> proved a callback for OOM conditions. Then, it will call your handler 
> function first.
> 
> I don't find it very onerous in my coding to say
> foo = new thing;
> if (!foo)
>    ....
> 

I've read about set_new_handler and the default behaviour of the 'new'
operator when allocationg fails is actually (based on
http://www.roguewave.com/support/docs/sourcepro/stdlibref/operatornew.html
):
 - see if there is a handler (previously specified with
   set_new_handler): successive calls to the handler and allocation
   tries until the handler fails to return (aborts) or the allocation
   succeeds
 - if it's a no_throw version: returns NULL
 - if it's the regular version: throws std::bad_alloc.

David, I don't know exactly if that was your point, but of all ways
presented here to avoid catching exceptions with the 'new' operator,
using 'set_new_hander' seems the best. It doesn't requires any include
for every source file where 'new' is used, and also avoids duplicating
the NULL check (already done internally by new) in every allocation. The
only bad thing is that we can't tell when an allocation failed, but the
out-of-memory condition shouldn't appear in practice, and I'd rather
have to fire gdb to find that out, than having to write "if(!(foo = new
..) == NULL) ..." around everywhere.

Thanks.

José Fonseca
__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com


-------------------------------------------------------
This SF.net email is sponsored by: Etnus, makers of TotalView, The debugger 
for complex code. Debugging C/C++ programs can leave you feeling lost and 
disoriented. TotalView can help you find your way. Available on major UNIX 
and Linux platforms. Try it free. www.etnus.com
_______________________________________________
Dri-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dri-devel

Reply via email to