If memory allocation using new is failed in C++ then how it should be handled? - GeeksforGeeks

20 Jul, 2021
In this article, if memory allocation using new is failed in C++ then how it should be handled? When an object of a class is created dynamically using new operator, the object occupies memory in the heap. Below are the major thing that must be keep in mind:
What if sufficient memory is not available in the heap memory, and how it should be handled?
If memory is not allocated then how to avoid the project crash?
Below is the program that occupies a large amount of memory so that the problem will occur. Use memory allocation statements in the try and catch block and for preventing memory crash and throw the exception when memory allocation is failed.
Program 1:
C++
// C++ program to illustrate memory// failure when very large memory// is allocated#include <iostream>using namespace std; // Driver Codeint main(){    // Allocate huge amount of memory    long MEMORY_SIZE = 0x7fffffff;     // Put memory allocation statement    // in the try catch block    try {        char* ptr = new char[MEMORY_SIZE];         // When memory allocation fails,        // below line is not be executed        // & control will go in catch block        cout << "Memory is allocated"             << " Successfully" << endl;    }     // Catch Block handle error    catch (const bad_alloc& e) {         cout << "Memory Allocation"             << " is failed: "             << e.what()             << endl;    }     return 0;}
Memory Allocation is failed: std::bad_alloc
 
The above memory failure issue can be resolved without using the try-catch block. It can be fixed by using nothrow version of the new operator:
The nothrow constant value is used as an argument for operator new and operator new[] to indicate that these functions shall not throw an exception on failure but return a null pointer instead.
By default, when the new operator is used to attempt to allocate memory and the handling function is unable to do so, a bad_alloc exception is thrown.
But when nothrow is used as an argument for new, and it returns a null pointer instead.
This constant (nothrow) is just a value of type nothrow_t, with the only purpose of triggering an overloaded version of the function operator new (or operator new[]) that takes an argument of this type.
Below is the implementation of memory allocation using nothrow operator:
Program 2:
C++
// C++ program to handle memory failure// when very large memory is allocated#include <iostream>using namespace std; // Drive Codeint main(){    // Allocate huge amount of memory    long MEMORY_SIZE = 0x7fffffff;     // Allocate memory dynamically    // using "new" with "nothrow"    // version of new    char* addr = new (std::nothrow) char[MEMORY_SIZE];     // Check if addr is having    // proper address or not    if (addr) {         cout << "Memory is allocated"             << " Successfully" << endl;    }    else {         // This part will be executed if        // large memory is allocated and        // failure occurs        cout << "Memory  allocation"             << " fails" << endl;    }     return 0;}
Memory  allocation fails
 
manikarora059
C++-new and delete
C-Dynamic Memory Allocation
memory-management
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Sorting a vector in C++
Friend class and function in C++
std::string class in C++
Pair in C++ Standard Template Library (STL)
Queue in C++ Standard Template Library (STL)
Inline Functions in C++
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++