Jump to content
  • Advertisement
Sign in to follow this  
beebs1

std::bad_alloc

This topic is 4089 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

Hiya, Does anyone know how to throw so that a std::bad_alloc exception is generated? throw std::bad_alloc; does not seem to work. I just need to test it's exception handler. Thanks for any help :)

Share this post


Link to post
Share on other sites
Advertisement
if you really need to just do a test, call something like
char *x = new char[unsigned long(-1)];

Share this post


Link to post
Share on other sites
The following works fine here:


#include <new>
#include <iostream>

int main()
{
try {
throw std::bad_alloc();
} catch (std::bad_alloc&) {
std::cout << "bad_alloc caught\n";
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by KulSeran
if you really need to just do a test, call something like
char *x = new char[unsigned long(-1)];


What makes you think that'll work? It may work in many cases, but I don't see why it have to throw a std::bad_alloc. Also if you have used set_new_handler it could result in your new_handler being called.

As long as you have included the "new" header you should be able to throw std::bad_alloc normally.

Share this post


Link to post
Share on other sites
Thanks for your replies, throw std::bad_alloc(); worked fine. I omitted the brackets for some reason, which is why it wouldn't compile. I just needed to test that particular code path.

Thanks again.

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!