如何在C++中删除指针数组
How to delete an array of pointers in C++?
我正试图消除代码中的内存泄漏,但每次我试图取消引用指针时,都会出现错误。
代码如下:
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()
相关文章:
- 无法删除指针,已触发断点
- 删除指针数组 (C++) 中的元素
- 如果我删除指针,我的C++程序会意外删除系统文件吗?
- 为什么const_cast删除指针的恒常性,而不删除指向常量的指针的恒常性?
- 删除指针后内存泄漏
- 这是系统资源吗?(或者我怎么知道我是否需要删除指针) - 在 C++ 中使用 C
- C++ 从 2 组指针中删除指针
- 当删除 [] 指针工作时,为什么无法获得指向的数组大小?
- 在这种情况下,删除指针数组期间会发生什么?
- 在C++中删除指针数组时析构函数崩溃
- 何时/如何在使用指针后正确删除指针?
- 在C++库中,谁应该删除指针、用户或库?
- 删除指针后阻止对象使用 "this"
- 删除指针后C++尝试/捕获
- 删除指针内存分配以避免内存泄漏
- 删除指针时,未分配被释放的 C++ 指针错误
- 如何从抽象类中删除指针
- 我应该在 v8::外部手动删除指针吗?
- 删除指针
- C 如何删除指针的默认对象成员