operator new and exception

S

seenutn

Hi All,
The standard form of operator new is to throw exception (bad_alloc)
when out of memory condition is encountered. But I want new to return
NULL instead of throwing exception (behavior similar to malloc). Any
idea of how to go about it?

TIA

Regards,
Seenu.
 
A

Alf P. Steinbach

* (e-mail address removed):
Hi All,
The standard form of operator new is to throw exception (bad_alloc)
when out of memory condition is encountered. But I want new to return
NULL instead of throwing exception (behavior similar to malloc). Any
idea of how to go about it?

#include <new>

int* = new(std::nothrow) int(42);
 
A

Adrian Hawryluk

Alf said:
* (e-mail address removed):

#include <new>

int* = new(std::nothrow) int(42);

Woah! Took a minute to figure out what your syntax meant. It looked
like you were doing something syntacticly bizarre.

You did mean:

int* pVarName = new(std::nothrow) int(42);

right? Pretty cool.


Adrian
--
_____________________________________________________________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under the Creative Commons /
\ Attribution-Noncommercial-Share Alike 3.0 License /
\_____[http://creativecommons.org/licenses/by-nc-sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top