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.....
http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=170
http://www.csci.csusb.edu/dick/c++std/cd2/expr.html#expr.new      --- Point 16

Following are some of the links that says the other story....
http://www.csci.csusb.edu/dick/c++std/cd2/expr.html#expr.new      --- Point 13
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_langref_new_operator.asp

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????

Regards,
Dennis
LVL 5
dennis_georgeAsked:
Who is Participating?

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

x
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.

Infinity08Commented:
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.

rajeev_devinCommented:
>> 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.
rajeev_devinCommented:
>> 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>
Infinity08Commented:
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.
rajeev_devinCommented:
I am saying VC++6.
Look at VC++7 and it supports the standard.
jkrCommented:
The idea is to provide your own 'new' handler, that works for all versions of VC++. See http://support.microsoft.com/kb/167733/en-us ("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 ... */
      _set_new_handler(_old_new_handler);
      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
Infinity08Commented:
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
jkrCommented:
>>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....
jkrCommented:
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....

Dennis
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
C++

From novice to tech pro — start learning today.