被释放的指针没有被分配 - 指针问题?

时间:2018-04-20 04:46:09

标签: c++ pointers error-handling

方法:

vector <Expression*> Addition :: getAdditiveTerms() {
vector <Expression*> terms;
if((obj1->getType() == "Addition" || obj1->getType() == "Subtraction") && (obj2->getType() == "Addition" || obj2->getType() == "Subtraction")) {
    if(obj1->getLeftSide()->getType() == "Integer") {
        Expression* LL = obj1->getLeftSide();
        terms.push_back(LL);
        delete obj1->getLeftSide();
    }
    if(obj1->getRightSide()->getType() == "Integer") {
        Expression* LR = obj1->getRightSide();
        terms.push_back(LR);
        delete obj1->getRightSide();
    }
    if(obj2->getLeftSide()->getType() == "Integer") {
        Expression* RL = obj2->getLeftSide();
        terms.push_back(RL);
        delete obj2->getLeftSide();
    }
    if(obj2->getRightSide()->getType() == "Integer") {
        Expression* RR = obj2->getRightSide();
        terms.push_back(RR);
        delete obj2->getRightSide();
    }
}
return terms;
}

当我创建一个对象并尝试访问此方法时,我收到以下错误:

  

ExpressionCalculator(19982,0x7fff8b43e380)malloc: *错误   对象0x7ffedfe729a8:未分配被释放的指针   * 在malloc_error_break中设置断点以进行调试

创建对象的主要内容:

int main() {
    Integer integ1("1");
    Integer integ2("-2");
    Integer* integer1 = &integ1;
    Integer* integer2 = &integ2;
    Addition add1(integer1, integer2);
    Integer integ3("6");
    Integer integ4("-9");
    Integer* integer3 = &integ3;
    Integer* integer4 = &integ4;
    Addition add2(integer3, integer4);
    Addition* addit1 = &add1;
    Addition* addit2 = &add2;

    Addition add3(addit1, addit2);

    vector <Expression*> addTerms = add3.getAdditiveTerms();

    for(int i = 0; i < addTerms.size(); i++)
        cout << addTerms[i]->getValue() << endl;

}

现在,我意识到我将这些指针引入构造函数的方式看起来有点奇怪,我必须制作指针以便以参数将在整个程序中传递的方式进行测试。我有什么想法可以收到此错误?我确定它与一个定义不明确的指针有关。谢谢!

1 个答案:

答案 0 :(得分:0)

你得到的错误很清楚,你试图删除一个引用你没有分配内存的对象的指针(使用new运算符)。

maingetAdditiveTerms函数的代码在指针上感觉很笨拙。您正在混合指针和非指针变量。如果您需要Addition构造函数中的指针,为什么不直接传递指针:

Integer* integer1 = new Integer("1");
Integer* integer2 = new Integer("-2");
Addition add1(integer1, integer2);

然后,你可以删除Integer desctructor中的Addition指针(这里似乎是最好的选择)或者你不再需要它们时。

关于getAdditiveTerms,为什么要在这里删除对象?在这种getter方法中删除指针感觉很奇怪。我想你可以删除那些删除语句。

总结一下:

// Remove delete statements
vector <Expression*> Addition :: getAdditiveTerms() {
vector <Expression*> terms;
if((obj1->getType() == "Addition" || obj1->getType() == "Subtraction") && (obj2->getType() == "Addition" || obj2->getType() == "Subtraction")) {
    if(obj1->getLeftSide()->getType() == "Integer") {
        Expression* LL = obj1->getLeftSide();
        terms.push_back(LL);
    }
    if(obj1->getRightSide()->getType() == "Integer") {
        Expression* LR = obj1->getRightSide();
        terms.push_back(LR);
    }
    if(obj2->getLeftSide()->getType() == "Integer") {
        Expression* RL = obj2->getLeftSide();
        terms.push_back(RL);
    }
    if(obj2->getRightSide()->getType() == "Integer") {
        Expression* RR = obj2->getRightSide();
        terms.push_back(RR);
    }
}
return terms;
}

// Use pointers
int main() {
    Integer* integer1 = new Integer("1");
    Integer* integer2 = new Integer("-2");
    Addition* add1 = new Addition(integer1, integer2);
    Integer* integer3 = new Integer("6");
    Integer* integer4 = new Integer("-9");
    Addition* add2 = new Addition(integer3, integer4);
    Addition* add3 = new Addition(addit1, addit2);

    vector <Expression*> addTerms = add3->getAdditiveTerms();

    for(int i = 0; i < addTerms.size(); i++)
        cout << addTerms[i]->getValue() << endl;

    delete add3;
    delete add2;
    delete add1;
    delete integer4;
    delete integer3;
    delete integer2;
    delete integer1;
}

使用Addition析构函数进行编辑

如果您的目标是使用Addition析构函数:

// In the Addition class, you need this
Addition::~Addition() {
    delete m_leftSide; // Customize with the proper attribute
    delete m_rightSide; // Customize with the proper attribute
}

// You don't need to delete all your pointers in the main now
int main() {
    Integer* integer1 = new Integer("1");
    Integer* integer2 = new Integer("-2");
    Addition* add1 = new Addition(integer1, integer2);
    Integer* integer3 = new Integer("6");
    Integer* integer4 = new Integer("-9");
    Addition* add2 = new Addition(integer3, integer4);
    Addition* add3 = new Addition(addit1, addit2);

    vector <Expression*> addTerms = add3->getAdditiveTerms();

    for(int i = 0; i < addTerms.size(); i++)
        cout << addTerms[i]->getValue() << endl;

    delete add3; // Will recusively delete all the pointers
}