Sobes.tech
Middle

What happens if the new operator cannot allocate memory?

sobes.tech AI

Answer from AI

The exception handler std::bad_alloc is called. By default, this handler terminates the program by calling std::terminate.

You can set your own handler using std::set_new_handler. It will be called if memory allocation fails. The handler should either free memory for a subsequent new attempt, throw an exception, or terminate the program (abort, exit).

There is also a non-throwing version of the new operator (new (std::nothrow)), which returns nullptr on failure.

#include <iostream>
#include <new>
#include <cstdlib>

// Custom handler
void my_new_handler() {
    std::cerr << "Memory allocation error!" << std::endl;
    // Attempt to free memory or terminate the program
    std::abort();
}

int main() {
    try {
        // Set custom handler
        std::set_new_handler(my_new_handler);

        // Attempt to allocate a large amount of memory
        int* large_array = new int[1000000000000000000ULL]; // Very large size
        
        // This code will not be reached if the handler is called successfully
        delete[] large_array;

    } catch (const std::bad_alloc& e) {
        // This block will execute if my_new_handler throws std::bad_alloc
        std::cerr << "Exception: " << e.what() << std::endl;
    }

    // Non-throwing version of new
    int* array_nothrow = new (std::nothrow) int[1000000000000000000ULL];
    if (array_nothrow == nullptr) {
        std::cerr << "Failed to allocate memory with new (std::nothrow)" << std::endl;
    } else {
        delete[] array_nothrow;
    }

    return 0;
}