Double in using 'new' operator

M

mthread

Hi,
I have a doubt about the use of 'new' operator. What happens when
a 'new' call fails. Does it return a NULL value or does it throw any
exception. I am a C programmer and just starting to learn C++. Kindly
let me know any resource(link or book) where I can get more
information for my doubt.
 
J

Juha Nieminen

mthread said:
Hi,
I have a doubt about the use of 'new' operator. What happens when
a 'new' call fails. Does it return a NULL value or does it throw any
exception. I am a C programmer and just starting to learn C++. Kindly
let me know any resource(link or book) where I can get more
information for my doubt.

If your compiler is standard-conforming then, AFAIK, it will throw an
exception (of the type std::bad_alloc). If you want it to return NULL
instead, you have to use new(nothrow).

Much older compilers might not throw on failure, so it cannot really
be assumed. Also some modern compilers might have an option to make it
always return NULL rather than throwing (even if (nothrow) is not used).
 

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,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top