Jump to content
  • Advertisement
Sign in to follow this  
Basiror

error handling for new operator?

This topic is 4838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I am working on a bitpacker for my library and need to allocate some buffers here and there but i wonder how i should react on a failure of "NEW" i could throw a bad alloc exception but is it worth it at all? in most cases the program will crash anyways when it runs out of memory any suggestions?

Share this post


Link to post
Share on other sites
Advertisement
Is your library intended to be used from both C or C++? Is it a source code library? Dynamically bound? What do you use internally for memory allocation? These are all factors in determining an error signalling mechanism for a library.

Share this post


Link to post
Share on other sites
Quote:
Original post by Basiror
Hi I am working on a bitpacker for my library and need to allocate some buffers here and there but i wonder how i should react on a failure of "NEW"
i could throw a bad alloc exception but is it worth it at all?

Given that the standard failure of new is to throw bad_alloc, probably not.

Personally, I only take the time to care about new failing in very specific circumstances... stuff like logging, that needs to work while the application is crashing down around it. Also, with STL and a good object management system, it's pretty rare to actually call new directly.

Share this post


Link to post
Share on other sites
its a source code library actually most of the code is in header files just like the STL

I use the standard new operators

i usually don t care about the result of the allocation either, since i am not working on industry projects

Share this post


Link to post
Share on other sites
In that case you might as well just rely on the fact that new throws std::bad_alloc. Just make sure your code is exception safe, and let your client deal with the exception.

Share this post


Link to post
Share on other sites
If a critical error happened in a constructor the best solution is throwing an exception (because the ctor does not have a return value ); however if the new does not allocate the memory requested ( and the amount is reasonable ) I think that throwing an exception is the right thing also in this case.
As you know you can throw and catch everything: simply when catch a 'new' failure terminate the application faster as you can ( and do not throw anything else in the destructors the 'rewinding' traverse ! )(that is: never throw an exception from a dtor...)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!