如何在C++中删除指针数组

How to delete an array of pointers in C++?

本文关键字:删除 指针 数组 C++      更新时间:2023-10-16

我正试图消除代码中的内存泄漏,但每次我试图取消引用指针时,都会出现错误。

代码如下:

exp[]在具有具体子类的抽象类Expression中声明如下:Binary、Constant和Log如下:static Expression*exp[];然后在表达式源文件中将其设置为NULL:表达式*表达式::exp[]={NULL};

当我注释掉删除b、删除l和删除co时,程序运行良好,但我怀疑它留下了内存泄漏

 int main(void)
    {
        char userInput='j';
        while (userInput != 'q' && userInput != 'Q')
        {       
        cout <<endl<<"Menu:" << endl<<"r - Reset the PC"<<endl<<"x - Enter a new value for register x"<<endl<<"y - Enter a new value for register y"<<endl<<"z - Enter a new value for register x"<<endl<<"s - Step by step execution"<<endl<<"e - Complete execution"<<endl<<"d - Display expressions"<<endl<<"q - Quit"<<endl;
            cin >> userInput;
            //Reset condition
            if (userInput == 'r' || userInput == 'R') 
            {
                debugPos=0;
                //Opening file and reading it line by line
                ifstream test("program.txt");
                string line;
                while (getline(test, line))
                {
                    //Removing spaces from the string
                    std::remove(line.begin(), line.end(), ' ');
                    //Check if it is a log type statement and create an object of Log if it is
                    if(line[2]=='l' || line[2]=='L')
                    {
                        Log *l = new Log(line); 
                        Expression::exp[Expression::count]= l;
                        Expression::count++;
                        delete l;
                    }
                    //Check if it is a binary type statement and create an object of Binary if it is
                    else if(line[2]=='x' || line[2]=='y' || line[2]=='z' || line[2]=='X' || line[2]=='Y' || line[2]=='Z')
                    {
                        Binary *b = new Binary(line); 
                        Expression::exp[Expression::count]= b;
                        Expression::count++;
                        delete b;
                    }
                    //Else create an object of Constant type
                    else
                    {
                        Constant *co = new Constant(line); 
                        Expression::exp[Expression::count]= co;
                        Expression::count++;
                        delete co;
                    }
                }
                test.close();
                cout<<endl<<"The expressions are read from the file"<<endl;
            }

            //Complete execution
            else if (userInput == 'e' || userInput == 'E')
            {
                Expression::nExecute();
                cout<<endl<<"Value of X: "<<Expression::X<<endl<<"Value of Y: "<<Expression::Y<<endl<<"Value of Z: "<<Expression::Z<<endl;
            }

            //Quit condition
            else if (userInput == 'q' || userInput == 'Q')
            {
                cout<<endl<<"Thanks for using the calculator"<<endl;
                break;
            }
            else
            {
                cout<<endl<<"Choose from the menu please"<<endl;
            }
        }
        return 0;
    }

此外,在重置条件下,我试图将指针重置为NULL,这样我就可以开始从文件中读取新的表达式,但我无法将数组设置为指向NULL

您会因为这个而出现错误

Constant *co = new Constant(line); 
Expression::exp[Expression::count]= co;
Expression::count++;
delete co;

分配内存,将指向数组中该内存位置的指针放置在数组中,然后释放该内存——因此,下次尝试取消引用存储在数组中的指针时,您正在访问释放的内存。如果你需要它们持续存在,只有在完成后才能删除它们。

更好:

Expression::exp.emplace_back(make_unique<Log>(line));
                                              //  ^^ new Log(line) is hidden inside here

等等,使用

std::vector<unique_ptr<Expression>> exp;

然后,当项目从向量中移除时,内存将被释放。而且它是例外安全的。

Expression::count将不再需要,因为您可以编写Expression::exp.size()