dennis_george
asked on
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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....
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....
>> 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.
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>
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That confirms it :) They were indeed following a post-1996 C++ standard, but chose not to follow it for this specific part.
ASKER
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>"..... 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.
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.
ASKER
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.
char* pc = new char[0xffffffff];
which would requre 4GB of free memory - not possible on 32bit systems.
ASKER
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
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
ASKER
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....