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

C++

Avatar of undefined
Last Comment
forums_mp

8/22/2022 - Mon
js-profi

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.


I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
habeeballah

I also tried

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

and it is working fine.

habeeballah

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.
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'.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
forums_mp

ASKER
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
habeeballah

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
habeeballah

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
forums_mp

ASKER
habeeballah answered the call