Link to home
Start Free TrialLog in
Avatar of forums_mp
forums_mp

asked on

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 ) 
  {

  }

}

Open in new window

Avatar of js-profi
js-profi

think you need to compile with a different preprocessor macro. will look for it.
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.
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.


I also tried

long *ptr = new ( std::nothrow ) long [max+1];

and it is working fine.

The reason for the above is that when you say

(( (2147483647 + 1 )/ 1024 )/ 1024 )/ 1024  you will get 2 GB. And this can be allocated in a clear block size I believe.
Avatar of forums_mp

ASKER

js-profi:
[where did you get the nothrownew.obj from? there also must be a (default) version of that object which doesnt prevent from throwing.]

http://msdn.microsoft.com/en-us/library/kftdy56f(VS.80).aspx
Now this is found on my machine under : 'C:\Program Files\Microsoft Visual Studio 9.0\VC\lib'.
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)
ASKER CERTIFIED SOLUTION
Avatar of habeeballah
habeeballah
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
habeeballah answered the call