gpt4 book ai didi

c++ - 尝试释放内存时出现 CrtIsValidHeapPointer 错误 [C++]

转载 作者:行者123 更新时间:2023-11-28 05:47:24 27 4
gpt4 key购买 nike

我有一个练习,我需要创建一个 Engine 类和一个 Car 类。

这些是我的部分代码:

汽车.cpp

Car::Car()
{
_engine = new Engine();
_manufacturer = new char[10];
_prod_year = 0;
_color = new char[10];
}

Car::~Car()
{
// Releasing allocated memory
delete[] _manufacturer;
delete[] _color;
delete _engine;
}

void Car::print(void) const
{
(*_engine).print();
cout << "\n" << endl;
cout << "Manufacturer:" << _manufacturer << "\n" << endl;
cout << "Production Year:" << _prod_year << "\n" << endl;
cout << "Color:" << _color << endl;
}

Car.h

class Car
{

Engine * _engine;
char * _manufacturer;
int _prod_year;
char * _color;

public:

/*
* Creates a Car with the default set of attributes.
*/

Car();
virtual ~Car();
inline Engine * GetEngine() { return _engine; }
void SetEngine(Engine * engine) { _engine = engine; }
inline char * GetManufacturer(){ return _manufacturer; }
void SetManufacturer(char * manufacturer){_manufacturer = manufacturer;}
inline int GetProdYear() { return _prod_year; }
void SetProdYear(int prod_year) { _prod_year = prod_year; }
inline char * GetColor() { return _color; }
void SetColor(char * color) { _color = color; }
void print(void) const;
};

Engine.h

class Engine
{
/*
Represents an Engine with the following attributes:
* int Hourse Power
* char * Manufacturer
* int Production Year
*/

int _horse_power;
char * _manufacturer;
int _prod_year;

public:
Engine();
virtual ~Engine();
int GetHorsePower() { return _horse_power; }
void SetHorsePower(int horse_power) { _horse_power = horse_power; }
char * GetManufacturer(){ return _manufacturer; }
void SetManufacturer(char * manufacturer){_manufacturer = manufacturer;}
int GetProdYear() { return _prod_year; }
void SetProdYear(int prod_year) { _prod_year = prod_year; }
void print() const;

};

Exc.cpp

Engine engine;
engine.SetHorsePower(150);
cout << "Current Horse Power: " <<engine.GetHorsePower()<<endl;

char * manufacturer = new char[strlen("VP") +1];
strcpy(manufacturer, "VP");
engine.SetManufacturer(manufacturer);
cout << "Current Manufacturer: " << engine.GetManufacturer() << endl;

engine.SetProdYear(1995);
cout << "Current Production Year: " << engine.GetProdYear() << endl;

cout << "\n ------------- Printing engine details -------------\n" << endl;
engine.print();
Car car;
car.SetEngine(&engine);
cout << "Current Engine: " << endl;
(*car.GetEngine()).print();

char * car_manufacturer = new char[strlen("Toyota") + 1];
car_manufacturer = { "Toyota" };
car.SetManufacturer(car_manufacturer);
cout << "Current Car Manufacturer: " << car.GetManufacturer() << endl;

car.SetProdYear(2010);
cout << "Current Car Production Year: " << car.GetProdYear() << endl;

char * color = new char[strlen("Yellow") + 1];
color = { "Yellow" };
car.SetColor(color);
cout << "Current Car Color: " << car.GetColor() << endl;

cout << "\n ------------- Printing car details -------------\n" << endl;

car.print();

我的程序运行正常,直到它到达 ~Car 然后我得到“CrtIsValidHeapPointer”。

谁能告诉我我做错了什么?

谢谢。


解决方案

Engine * engine = new Engine;
engine->SetHorsePower(150);
cout << "Current Horse Power: " << engine->GetHorsePower()<<endl;

char * manufacturer = new char[strlen("VP") +1];
strcpy(manufacturer, "VP");
engine->SetManufacturer(manufacturer);
cout << "Current Manufacturer: " << engine->GetManufacturer() << endl;

engine->SetProdYear(1995);
cout << "Current Production Year: " << engine->GetProdYear() << endl;

cout << "\n ------------- Printing engine details -------------\n" << endl;
engine->print();

Car car;
car.SetEngine(engine);
cout << "Current Engine: " << endl;
car.GetEngine()->print();

char * car_manufacturer = new char[strlen("Toyota") + 1];
strcpy(car_manufacturer, "Toyota");
car.SetManufacturer(car_manufacturer);
cout << "Current Car Manufacturer: " << car.GetManufacturer() << endl;

car.SetProdYear(2010);
cout << "Current Car Production Year: " << car.GetProdYear() << endl;

char * color = new char[strlen("Yellow") + 1];
strcpy(color, "Yellow");
car.SetColor(color);
cout << "Current Car Color: " << car.GetColor() << endl;

cout << "\n ------------- Printing car details -------------\n" << endl;

car.print();

return 0;

最佳答案

Car 构造函数中,您动态分配一个 Engine 并分配给 _engine

然后在你的主程序中做

car.SetEngine(&engine);

从而改变 _engine 指向一个你还没有new 动态分配的对象,并丢失原始指针并给你一个内存泄漏。

因此,在 Car 析构函数中,您尝试删除未使用 new 分配的指针,导致未定义的行为

字符串也有问题,你可以在这里做类似的事情

char * car_manufacturer = new char[strlen("Toyota") + 1];
car_manufacturer = { "Toyota" };

首先分配内存,然后您将指向该内存的指针分配给 car_manufacturer。紧接着你让 car_manufacturer 指向 其他地方 再次失去指向你分配的内存的指针和新的内存泄漏。此外,您还可以将 car 对象中的指针设置为字符串文字 "Toyota" ,这又是指向您尚未使用 new[] 分配的内存的指针 再次执行 delete[] 会导致未定义的行为


使用字符串解决问题很容易,即使您不想使用 std::string(这是推荐的解决方案),也就是复制字符串 而不是重新分配指针:

char * car_manufacturer = new char[strlen("Toyota") + 1];
strcpy(car_manufacturer, "Toyota");

要解决引擎的第一个问题,您还需要动态分配新引擎,如果您调用SetEngine

在您的 Set 函数中,您还应该释放已经分配的内存,这样就不会发生内存泄漏。

关于c++ - 尝试释放内存时出现 CrtIsValidHeapPointer 错误 [C++],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35958771/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com