如何在此 C++ 程序中正确释放动态内存?执行程序时出现分段错误(核心转储)
How do I deallocate the dynamic memory correctly in this C++ program? I get a segmentation fault (core dump) when I execute the program
如何在这个简单的 C++ 程序中正确释放动态内存?执行程序时出现分段错误(核心转储)。我想找出我哪里出错了。提前致谢。
我正在尝试将动态分配的对象添加到数组中,并试图让析构函数清除内存。我不确定我是否在 carList 中正确创建了析构函数。
#include <iostream>
#define MAX 3
class Car{
public:
Car(std::string name){
this->name = name;
}
~Car(){
}
std::string getName(){
return this->name;
}
private:
std::string name;
};
class carList{
public:
carList(){
this->length = 0;
}
~carList(){
//the deconstructer; I am unsure If I have implemented it correctly
for (int i = 0; i < this->length; i++)
{
if(listCar[i] != nullptr){
delete[] listCar[i];
}
}
}
int getLength(){
return this->length;
}
// adding to the end of the list, unsure whether Implemented correctly
void addEndList(Car* car){ //???
if(this->length < MAX){
this->listCar[length+1] = car;
this->length++;
}
}
private:
Car* listCar[MAX];
int length;
};
int main(){
carList* carlist = new carList();
std::cout << carlist->getLength() <<std::endl;
Car* car2 = new Car("one");
Car* car3 = new Car("two");
carlist->addEndList(car2);
carlist->addEndList(car3);
std::cout << carlist->getLength() <<std::endl;
std::string name1 = car2->getName();
std::cout << name1 << std::endl;
delete carlist; // deleting the carlist, unsure if I need to delete car2,
//and car3 or if the destructor handles that, which I am
//trying to implement
return 0;
}
数组索引在 C++ 中从 0
开始。所以,
this->listCar[length+1] = car;
应该是
this->listCar[length] = car;
否则你不初始化索引0
,而是delete
它。
第二个问题是 delete
。您应该对 new
返回的指针使用 delete
,对 new[]
返回的指针使用 delete[]
。您使用了 new
。所以,
delete[] listCar[i];
应该是
delete listCar[i];
经过这 2 次更改后看起来没问题:https://godbolt.org/z/84M1fq1fv
如何在这个简单的 C++ 程序中正确释放动态内存?执行程序时出现分段错误(核心转储)。我想找出我哪里出错了。提前致谢。
我正在尝试将动态分配的对象添加到数组中,并试图让析构函数清除内存。我不确定我是否在 carList 中正确创建了析构函数。
#include <iostream>
#define MAX 3
class Car{
public:
Car(std::string name){
this->name = name;
}
~Car(){
}
std::string getName(){
return this->name;
}
private:
std::string name;
};
class carList{
public:
carList(){
this->length = 0;
}
~carList(){
//the deconstructer; I am unsure If I have implemented it correctly
for (int i = 0; i < this->length; i++)
{
if(listCar[i] != nullptr){
delete[] listCar[i];
}
}
}
int getLength(){
return this->length;
}
// adding to the end of the list, unsure whether Implemented correctly
void addEndList(Car* car){ //???
if(this->length < MAX){
this->listCar[length+1] = car;
this->length++;
}
}
private:
Car* listCar[MAX];
int length;
};
int main(){
carList* carlist = new carList();
std::cout << carlist->getLength() <<std::endl;
Car* car2 = new Car("one");
Car* car3 = new Car("two");
carlist->addEndList(car2);
carlist->addEndList(car3);
std::cout << carlist->getLength() <<std::endl;
std::string name1 = car2->getName();
std::cout << name1 << std::endl;
delete carlist; // deleting the carlist, unsure if I need to delete car2,
//and car3 or if the destructor handles that, which I am
//trying to implement
return 0;
}
数组索引在 C++ 中从 0
开始。所以,
this->listCar[length+1] = car;
应该是
this->listCar[length] = car;
否则你不初始化索引0
,而是delete
它。
第二个问题是 delete
。您应该对 new
返回的指针使用 delete
,对 new[]
返回的指针使用 delete[]
。您使用了 new
。所以,
delete[] listCar[i];
应该是
delete listCar[i];
经过这 2 次更改后看起来没问题:https://godbolt.org/z/84M1fq1fv