new - bad_alloc or NULL

Hi all,

I was in the assumption that "new" by default throws an exception and if you want "new" to return NULL you use "std::nothrow"....

So I like to know that whether my assumption is correct or not......

Following are some of the links that supports my argument.....      --- Point 16

Following are some of the links that says the other story....      --- Point 13

So can you tell me who is correct... basically I am confused by the point-13 and point-16 of the standard.....
So what will I expect in the compilers I will be using????

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

In the early days of C++, it was done the C way - ie. NULL was returned when the allocation failed. This can generate a lot of (difficultly traceable) bugs when the programmer forgets to do this. The exception throwing/handling system is more suited to this task, and that is why later on (if i'm not mistaken in 1996) the new operator throwed a bad_alloc exception instead of returning NULL. Most of today's compilers use the latter scheme ... to make sure, just check what standard the compiler conforms to.

regarding the nothrow directive in the later C++ standards :

  p = new(nothrow) char;

will act the C way - ie. it will not throw a bad_alloc exception, but will return NULL on failure.

  p = new char;

will throw a bad_alloc exception on failure.
dennis_georgeAuthor Commented:
Hi Infinity08,

I also have the same opinion..... but then y does the MSDN says "If unsuccessful, by default new returns zero"...

Does that means VS6 follows the old C++ rules....
dennis_georgeAuthor Commented:
The c++ standard link given in my first post is refering to a 1996 draft..... And since C++ was standardized on 1998, it may not be a correct link to post......

But this shows that VC++ 6.0 follows pre 1998 standards...... So if I am coding in VC++ 6.0 I have to check the return value of "new".... and if I am coding on some later compilers I don't have to check the return value....
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

>> I also have the same opinion..... but then y does the MSDN says "If unsuccessful, by default new returns zero"...
If you are using VC++6 then remember that it is an old compiler.
>> So if I am coding in VC++ 6.0 I have to check the return value of "new"
To make the code similer use the new operator provided in
#include <new>
Visual C++ isn't notorious for following the specifications, so it probably means that they chose to implement it this way (against the standard). I don't think they based themselves on an early C++ standard, although i could be mistaken.
I am saying VC++6.
Look at VC++7 and it supports the standard.
The idea is to provide your own 'new' handler, that works for all versions of VC++. See ("Operator new does not throw a bad_alloc exception on failure in Visual C++"):

   #include <new>
   #include <new.h>
   int my_new_handler(size_t) {
      throw std::bad_alloc();
      return 0;
   int main () {
      _PNH _old_new_handler;
      _old_new_handler = _set_new_handler(my_new_handler);
      /* ... application processing ... */
      return 0;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
That confirms it :) They were indeed following a post-1996 C++ standard, but chose not to follow it for this specific part.
dennis_georgeAuthor Commented:
Hi jkr...

Why did you included both "<new>" and "<new.h>"..... If I just include "<new>" won't the failure of "new" throw bad_alloc ?
or it will still return me NULL in VC++ 6
>>Why did you included both "<new>" and "<new.h>"

That was a MS sample, I guess they wanted to cover everything. Anyway, if you set your own handler, you can decide what action you want to take - throw an exception or have 'new' return NULL.
dennis_georgeAuthor Commented:
Is there any way by which I can emulate a "no-memory" scenario.... so that I can confirm it by myself....
Sure, just try to

char* pc = new char[0xffffffff];

which would requre 4GB of free memory - not possible on 32bit systems.
dennis_georgeAuthor Commented:
0xffffffff aborts the program itself.... but anyway i tried with a lesser amount... 0xefffffff, then it worked.... thanks jkr... I thought there will be some debug code available which can emulate the scenario..... anyways..... I tested in my VC++ environment...... it returns NULL.... and you have to include new.h to call _set_new_handler...

Even if you include <new> during allocation failure it returns NULL. So to make sure that your new throws an exception in VC++ you to call _set_new_handler and in the handler function you have to throw an exception as shown by jkr....

Thanks guys for the whole discussion....

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.