when memory exhaust,how we can catch the new operator exception.

  • Thread starter 8519428 Wang Tian-da Alex
  • Start date
8

8519428 Wang Tian-da Alex

when memory exhaust,how we can catch the new operator exception.
 
M

Mike Wahler

8519428 Wang Tian-da Alex said:
when memory exhaust,how we can catch the new operator exception.

#include <iostream>
#include <new>

int main()
{
const size_t elem_count(1000);
const size_t elem_size(1000);
char *p = 0;

try
{
p = new char[elem_count * elem_size];
}
catch(std::bad_alloc& e)
{
std::cerr << e.what() << '\n';
return EXIT_FAILURE;
}

delete[] p;
return EXIT_SUCCESS;
}

-Mike
 
8

8519428 Wang Tian-da Alex

Mike,

If I don't use Sun C++ 4.2, how to catch the new error.
Thanks,
Alex Wang.
 
M

Mike Wahler

8519428 Wang Tian-da Alex said:
Mike,

If I don't use Sun C++ 4.2, how to catch the new error.

The code I posted will work with any standard-compliant
compiler.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top