为什么我不能使用"delete"关键字回收动态分配的内存?

syn*_*tik 3 c++ memory-management new-operator

我有以下课程:

class Patient {
public:
    Patient(int x);
    ~Patient();
private:
    int* RP;
};

Patient::Patient(int x) { RP = new int [x]; }
Patient::~Patient() { delete [] RP; }
Run Code Online (Sandbox Code Playgroud)

我在堆栈上创建了这个类的实例,如下所示:

void f() { Patient p(10); }
Run Code Online (Sandbox Code Playgroud)

现在,当f()返回时,我得到一个"双重免费或损坏"错误,它向我发出信号,表示尝试删除的内容不止一次.但我不明白为什么会这样.数组的空间是在堆上创建的,只是因为分配了空间的函数返回,我不希望回收空间.

我认为如果我在堆上分配空间(使用new关键字),那么回收该空间的唯一方法是使用delete关键字.救命!

根据要求,这是实际的代码(为简洁起见略有删节)

这是完整的类定义(拆分.cpp.h文件,但一起显示):

class Patient {

public:
    Patient(int numDecisionEpochs);

    ~Patient();

    void recordRP(const int& age, const bool& t);
    void recordBiopsy(const int& age, const int& result);
    void recordPSA(const int& age, const double& level);
    void recordPSA(const int& age);
private:
    int* RP;
    int* Biopsy;
    double* PSA;
};

Patient::Patient(int numDecisionEpochs) {
    RP = new int [numDecisionEpochs];
    Biopsy = new int [numDecisionEpochs];
    PSA = new double [numDecisionEpochs];
}

Patient::~Patient() {
    delete[] RP;
}

void Patient::recordRP(const int& age, const bool& t) {
    if(t)
    RP[age-1] = 1;  // RP either yes (1) or no (0)
    else
    RP[age-1] = 0;
}

void Patient::recordBiopsy(const int& age, const int& result) {
    switch(result)
    {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
        Biopsy[age-1]=result; // only permit results 0,1,2,3,4
        break;
    default:
        cerr << "Invalid biopsy result (" << result << ") at age " << age << "!\n";
    }
}

void Patient::recordPSA(const int& age, const double& level) {
    PSA[age-1] = level; // record PSA volume
}

void Patient::recordPSA(const int& age) {
    PSA[age-1] = -1; // symbol for no screening during epoch
}
Run Code Online (Sandbox Code Playgroud)

接下来,使用上述类的函数.直接调用以下函数main()并传递一个Policy完全独立且与Patient类分离的对象:

void simulate1(Policy& P)
{
    // ...
    Patient patient(260);

    for(int idx=0; idx<(P.size); idx++)
    {
        while(state != 9) // while patient not dead
        {
                // ...
                patient.recordPSA(age,PSA);
                // ...
                patient.recordPSA(age);
                // ...
                patient.recordBiopsy(age,biopsyResult);
                // ...
                patient.recordRP(age,true);
                // ...
                patient.recordRP(age,false);
                // ...

        } // end patient (while loop)

    } // end sample (for loop)

} // end function
Run Code Online (Sandbox Code Playgroud)

ild*_*arn 7

你违反了三条规则(或者对于C++ 11,五条规则).您需要一个复制构造函数和复制赋值运算符来执行指针的深层复制.当然,由于您没有跟踪分配的数组大小,因此如果不引入第二个数据成员,则无法实现.