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

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:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustarate memory
// failure when very large memory
// is allocated
#include <iostream>
using namespace std;
  
// Driver Code
int 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;
}
chevron_right

Output:
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:



Below is the implementation of memory allocation using nothrow operator:

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to handle memory failure
// when very large memory is allocated
#include <iostream>
using namespace std;
  
// Drive Code
int 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;
}
chevron_right

Output:
Memory  allocation fails

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
C++
Practice Tags :