如果在释放新运算符分配的内存之前会发生什么,会发生异常?

Jay*_*esh 0 c++ free new-operator dynamic-memory-allocation

我只是很想知道,如果在释放新运算符分配的内存之前会发生什么,会发生异常?是否发生了内存泄漏问题?

#include <iostream>
#include<new>

using namespace std;

void func()
{
    try
    {
        int *p = new int[10];

        /*
            Number of lines code here
            .
            .
            .
            .
            .
            Suppose here I got exception then What heppens????
            .
            .
            .
            .
        */
        delete []p;
    }
    catch(const std::exception& e)
    {
        cout<<"Exception occured"<<endl;
    }
}

int main() {
    func();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Sto*_*ica 6

是否发生了内存泄漏问题?

是的.这就是设计智能指针和整个RAII成语的全部原因.在找到处理程序时仍会调用块作用域变量的析构函数,因此可以释放分配的资源.原始指针只会泄漏.