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

operator new and exception

P: n/a
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.

Mar 19 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
* se*****@gmail.com:
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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 19 '07 #2

P: n/a
Alf P. Steinbach wrote:
* se*****@gmail.com:
>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);
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/...sa/3.0/]_____/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
Mar 19 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.