• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 849
  • Last Modified:

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
0
dennis_george
Asked:
dennis_george
  • 5
  • 3
  • 3
  • +1
3 Solutions
 
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.
0
 
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....
0
 
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....
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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.
0
 
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>
0
 
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.
0
 
rajeev_devinCommented:
I am saying VC++6.
Look at VC++7 and it supports the standard.
0
 
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;
   }
0
 
Infinity08Commented:
That confirms it :) They were indeed following a post-1996 C++ standard, but chose not to follow it for this specific part.
0
 
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
0
 
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.
0
 
dennis_georgeAuthor Commented:
Is there any way by which I can emulate a "no-memory" scenario.... so that I can confirm it by myself....
0
 
jkrCommented:
Sure, just try to

char* pc = new char[0xffffffff];

which would requre 4GB of free memory - not possible on 32bit systems.
0
 
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
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 5
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now