'Debug Assertion Failed' 使用 'delete' 运算符清理内存时出错
'Debug Assertion Failed' Error when cleaning memory using 'delete' operator
我已经尝试了所有方法,但代码不起作用,我不明白为什么。
我有两个 class。
这是基础 class:
class Vegetables
{
private:
char *nameStr;
char *countrySrc;
int seasonRd;
public:
Vegetables()
{
cout << "Default constructor for Vegetables" << endl;
nameStr = new char[20];
nameStr = "Unknown";
countrySrc = new char[20];
countrySrc = "Unknown";
seasonRd = -1;
}
virtual ~Vegetables()
{
delete[]nameStr; //Here happens the error (_crtisvalidheappointer(block))
delete[]countrySrc;
cout << "Destructor for Vegetables" << endl;
}
};
继承了class 'Inherited Unit':
class InhUnit : public Vegetables
{
private:
Delivery delivery_;
Vegetables vegetables;
int quantity;
int price;
int delivPrice;
public:
InhUnit() :Vegetables(),delivery_(OwnCosts), vegetables(), quantity(-1), price(-1), delivPrice(-1)
{
cout << "Default constructor for Inherited Unit" << endl;
}
~InhUnit()
{
cout << "Destructor for Inherited Unit" << endl;
}
};
弹出此错误的原因可能是什么?
这不是您复制字符串的方式,请改用 strcpy
Vegetables()
{
cout << "Default constructor for Vegetables" << endl;
nameStr = new char[20];
strcpy(nameStr, "Unknown");
countrySrc = new char[20];
strcpy(countrySrc, "Unknown");
seasonRd = -1;
}
您正在做的是分配一些内存并将其分配给一个指针。然后在下一行中,您将指针分配给指向一个字符串,而不是将字符串复制到您分配的内存中。
当你调用 delete[]
因为指针没有指向你分配的内存时你得到了一个错误。
您应该像 std::string 那样使用字符串 类 来避免此类指针问题。
更正代码
class Vegetables {
private:
std::string nameStr; // Use std::string instead of C-style string
std::string countrySrc;
int seasonRd;
public:
// Use constructor initialization list
Vegetables() : nameStr("Unknown"), countrySrc("Unknown"), seasonRd(-1) {
cout << "Default constructor for Vegetables" << endl;
}
virtual ~Vegetables() {
cout << "Destructor for Vegetables" << endl;
}
};
我已经尝试了所有方法,但代码不起作用,我不明白为什么。
我有两个 class。 这是基础 class:
class Vegetables
{
private:
char *nameStr;
char *countrySrc;
int seasonRd;
public:
Vegetables()
{
cout << "Default constructor for Vegetables" << endl;
nameStr = new char[20];
nameStr = "Unknown";
countrySrc = new char[20];
countrySrc = "Unknown";
seasonRd = -1;
}
virtual ~Vegetables()
{
delete[]nameStr; //Here happens the error (_crtisvalidheappointer(block))
delete[]countrySrc;
cout << "Destructor for Vegetables" << endl;
}
};
继承了class 'Inherited Unit':
class InhUnit : public Vegetables
{
private:
Delivery delivery_;
Vegetables vegetables;
int quantity;
int price;
int delivPrice;
public:
InhUnit() :Vegetables(),delivery_(OwnCosts), vegetables(), quantity(-1), price(-1), delivPrice(-1)
{
cout << "Default constructor for Inherited Unit" << endl;
}
~InhUnit()
{
cout << "Destructor for Inherited Unit" << endl;
}
};
弹出此错误的原因可能是什么?
这不是您复制字符串的方式,请改用 strcpy
Vegetables()
{
cout << "Default constructor for Vegetables" << endl;
nameStr = new char[20];
strcpy(nameStr, "Unknown");
countrySrc = new char[20];
strcpy(countrySrc, "Unknown");
seasonRd = -1;
}
您正在做的是分配一些内存并将其分配给一个指针。然后在下一行中,您将指针分配给指向一个字符串,而不是将字符串复制到您分配的内存中。
当你调用 delete[]
因为指针没有指向你分配的内存时你得到了一个错误。
您应该像 std::string 那样使用字符串 类 来避免此类指针问题。
更正代码
class Vegetables {
private:
std::string nameStr; // Use std::string instead of C-style string
std::string countrySrc;
int seasonRd;
public:
// Use constructor initialization list
Vegetables() : nameStr("Unknown"), countrySrc("Unknown"), seasonRd(-1) {
cout << "Default constructor for Vegetables" << endl;
}
virtual ~Vegetables() {
cout << "Destructor for Vegetables" << endl;
}
};