Linking with "nothrownew.obj" (MSVC Express)
Code referenced below is generating an exception in MSVC 2010 express. According to Microsoft I must link with '"nothrownew.obj" if I want the non throwing version of new. So I added (I think) nothrownew.obj. To do this I did:
1) Right click on the project name
2) Select Properties
3) Select Linker menu
4) Added "nothrownew.obj" (with quotes) to Additional Options field.
5) Depressed Ok
Compile and ran program but code still generates 'exception'. IOW: It appears I'm not using the nothrow form of new. The end result is the same if I used MSVC express 2008. What am I missing in my steps?
# include <iostream># include <numeric>int main() { long const max = std::numeric_limits < long >::max(); long *ptr = new ( std::nothrow ) long [ max ]; if ( ptr ) { }}
think you need to compile with a different preprocessor macro. will look for it.
js-profi
the only difference i could see to example code is that they were including <new>
where did you get the nothrownew.obj from? there also must be a (default) version of that object which doesnt prevent from throwing.
habeeballah
hi,
The actual problem is coming from allocating the array of the calculated size.
long const max = std::numeric_limits < long >::max();
gives you 2147483647.
The C runtime that you have on your machine, doesn't allow you to declare an array of this size but you can declare up to the size of 2147483631. You can test it like this
long *ptr = new ( std::nothrow ) long [ 2147483631 ];
and then increase the size by 1 and you will again get the same assertion error.
habeeballah:
[The C runtime that you have on your machine, doesn't allow you to declare an array of this size but you can declare up to the size of 2147483631. You can test it like this]
And that's fine but I should not get an assertion. If I wrap a try / catch ( std::bad_alloc) around the code I get a std::bad_alloc - which is the default behavior for operator new. I want to test the nothrow version and my question to you then - since it appears code works for you - is what did u do (walk me through the menu options) to link wiht nothrownew.obj? (i.e how did u include nothrownew.obj in your project)