How to deal with bad_alloc in C++?

There is a method called foo that sometimes returns the following error:

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Is there a way that I can use a try-catch block to stop this error from terminating my program (all I want to do is return -1)?

If so, what is the syntax for it?

How else can I deal with bad_alloc in C++?


You can catch it like any other exception:

try {
catch (const std::bad_alloc&) {
  return -1;

Quite what you can usefully do from this point is up to you, but it's definitely feasible technically.

In general you cannot and should not try to respond to this error. bad_alloc indicates that a resource cannot be allocated because not enough memory is available. In most scenarios your program cannot hope to cope with that, and terminating soon is the only meaningful behaviour.

Worse, modern operating systems often over-allocate: malloc and new will always return a valid pointer, even if there is technically no (or not enough) free memory left – so std::bad_alloc will never be thrown, or is at least not a reliable sign of memory exhaustion. Instead, attempts to access the allocated memory will then result in an error, which is not catchable.

The only thing you could do when catching std::bad_alloc is to perhaps log the error, and try to ensure a safe program termination by freeing outstanding resources (but this is done automatically in the normal course of stack unwinding after the error gets thrown if the program uses RAII appropriately).

In certain cases the program may attempt to free some memory and try again, or use secondary memory (= disk) instead of RAM but these opportunities only exists in very specific scenarios.

What is the C++ Standard specified behavior of new in c++?

The usual notion is that if new operator cannot allocate dynamic memory of the requested size, then it should throw an exception of type std::bad_alloc. However, something more happens even before a bad_alloc exception is thrown:

C++03 Section says

An allocation function that fails to allocate storage can invoke the currently installed new_handler(, if any. [Note: A program-supplied allocation function can obtain the address of the currently installed new_handler using the set_new_handler function (] If an allocation function declared with an empty exception-specification (15.4), throw(), fails to allocate storage, it shall return a null pointer. Any other allocation function that fails to allocate storage shall only indicate failure by throw-ing an exception of class std::bad_alloc ( or a class derived from std::bad_alloc.

Consider the following code sample:

#include <iostream>
#include <cstdlib>

// function to call if operator new can't allocate enough memory or error arises
void outOfMemHandler()
    std::cerr << "Unable to satisfy request for memory\n";


int main()
    //set the new_handler

    //Request huge memory size, that will cause ::operator new to fail
    int *pBigDataArray = new int[100000000L];

    return 0;

In the above example, operator new (most likely) will be unable to allocate space for 100,000,000 integers, and the function outOfMemHandler() will be called, and the program will abort after issuing an error message.

As seen here the default behavior of new operator when unable to fulfill a memory request, is to call the new-handler function repeatedly until it can find enough memory or there is no more new handlers. In the above example, unless we call std::abort(), outOfMemHandler() would be called repeatedly. Therefore, the handler should either ensure that the next allocation succeeds, or register another handler, or register no handler, or not return (i.e. terminate the program). If there is no new handler and the allocation fails, the operator will throw an exception.

What is the new_handler and set_new_handler?

new_handler is a typedef for a pointer to a function that takes and returns nothing, and set_new_handler is a function that takes and returns a new_handler.

Something like:

typedef void (*new_handler)();
new_handler set_new_handler(new_handler p) throw();

set_new_handler's parameter is a pointer to the function operator new should call if it can't allocate the requested memory. Its return value is a pointer to the previously registered handler function, or null if there was no previous handler.

How to handle out of memory conditions in C++?

Given the behavior of newa well designed user program should handle out of memory conditions by providing a proper new_handlerwhich does one of the following:

Make more memory available: This may allow the next memory allocation attempt inside operator new's loop to succeed. One way to implement this is to allocate a large block of memory at program start-up, then release it for use in the program the first time the new-handler is invoked.

Install a different new-handler: If the current new-handler can't make any more memory available, and of there is another new-handler that can, then the current new-handler can install the other new-handler in its place (by calling set_new_handler). The next time operator new calls the new-handler function, it will get the one most recently installed.

(A variation on this theme is for a new-handler to modify its own behavior, so the next time it's invoked, it does something different. One way to achieve this is to have the new-handler modify static, namespace-specific, or global data that affects the new-handler's behavior.)

Uninstall the new-handler: This is done by passing a null pointer to set_new_handler. With no new-handler installed, operator new will throw an exception ((convertible to) std::bad_alloc) when memory allocation is unsuccessful.

Throw an exception convertible to std::bad_alloc. Such exceptions are not be caught by operator new, but will propagate to the site originating the request for memory.

Not return: By calling abort or exit.

I would not suggest this, since bad_alloc means you are out of memory. It would be best to just give up instead of attempting to recover. However here is is the solution you are asking for:

try {
} catch ( const std::bad_alloc& e ) {
    return -1;

I may suggest a more simple (and even faster) solution for this. new operator would return null if memory could not be allocated.

int fv() {
    T* p = new (std::nothrow) T[1000000];
    if (!p) return -1;
    delete p;
    return 0;

I hope this could help!

Let your foo program exit in a controlled way:

#include <stdlib.h>     /* exit, EXIT_FAILURE */

try {
} catch (const std::bad_alloc&) {

Then write a shell program that calls the actual program. Since the address spaces are separated, the state of your shell program is always well-defined.

Need Your Help

How can I remove a column from a sparse matrix efficiently?

python matrix numpy scipy algebra

If I am using the sparse.lil_matrix format, how can I remove a column from the matrix easily and efficiently?

Jquery Showcase how not to return to the first item on finish

javascript jquery html

I have a bunch of images being shown using JQuery Showcase. Everythin works just fine except one thing - I need it continue sliding when after it shows last item - so it keeps going from right to l...