📌  相关文章
📜  如果在C++中使用new分配内存失败,那么应该如何处理呢?

📅  最后修改于: 2021-05-30 14:37:16             🧑  作者: Mango

在本文中,如果在C++中使用new分配内存失败,那么应该如何处理呢?当使用new运算符动态创建类的对象时,该对象将占用堆中的内存。以下是必须牢记的主要内容:

  • 如果堆内存中没有足够的内存怎么办?应该如何处理呢?
  • 如果未分配内存,那么如何避免项目崩溃?

下面是占用大量内存的程序,因此会出现此问题。在try and catch块中使用内存分配语句,以防止内存崩溃并在内存分配失败时引发异常。

程序1:

C++
// C++ program to illustarate memory
// failure when very large memory
// is allocated
#include 
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;
}


C++
// C++ program to handle memory failure
// when very large memory is allocated
#include 
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;
}


输出:
Memory Allocation is failed: std::bad_alloc

无需使用try-catch块,即可解决上述内存故障问题。可以使用new运算符的nothrow版本进行修复:

  • nothrow常量值用作运算符new和运算符 new []的参数,以指示这些函数在失败时不会引发异常,而是返回空指针。
  • 默认情况下,当使用new运算符尝试分配内存而处理函数无法这样做时,将引发bad_alloc异常。
  • 但是当nothrow用作new的参数时,它将返回一个空指针。
  • 该常数(nothrow)只是nothrow_t类型的值,其唯一目的是触发采用此类型参数的函数运算符 new(或运算符 new [])的重载版本。

下面是使用nothrow运算符实现的内存分配:

程式2:

C++

// C++ program to handle memory failure
// when very large memory is allocated
#include 
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;
}
输出:
Memory  allocation fails
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”