Doubt 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.
 
T

Triple-DES

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.

The regular operator new will throw an std::bad_alloc exception upon
failure. But there's also a nothrow version that returns 0 upon
failure.

int * p = new int; // may throw std::bad_alloc

#include <new>
int * q = new(nothrow) int; // may return 0

I believe most C++ textbooks cover this.
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top