gpt4 book ai didi

C++ double free or corruption (out) : Even with copy constructor and assignment operator

转载 作者:搜寻专家 更新时间:2023-10-31 00:56:39 25 4
gpt4 key购买 nike

我的代码正在生成* `./a.out' 中的错误:双重释放或损坏(输出):0x00007ffe400eb0e0 *

无论何时运行,我都认为这是一个基于我的复制构造函数或我如何删除动态数组的问题,但我无法确定问题出在哪里:

我的类(class):

class Student {
public:
Student();
Student(const Student&);
Student & operator= (const Student&);
~Student();
void setStudentData(int *, int &, int, string);
int getNumOfSubjTaken();
int getAverageMark();
int getLowestMark();
int getHighestMark();
string getFullName();
void sortMarks(int &);

private:
string fullName;
int *marks;
int numOfSubjects;

};

复制构造函数:

Student::Student(const Student& pupil) {
marks = new int[numOfSubjects = pupil.numOfSubjects];
for (int i = 0; i < numOfSubjects; i++) {
marks[i] = pupil.marks[i];
}
fullName = pupil.fullName;
//removed after edit: marks = pupil.marks;
}

赋值运算符:

Student &Student::operator=(const Student &pupil) {
if (this != &pupil) {
for (int i = 0; i < numOfSubjects; i++) {
marks[i] = pupil.marks[i];
}
fullName = pupil.fullName;
numOfSubjects = pupil.numOfSubjects;
marks = pupil.marks;
}
return *this;

解构器:

Student::~Student(){
if (marks != NULL) {
delete [] marks;
}
marks = NULL;
numOfSubjects = 0;
fullName = "";
}

设置函数(修改器):

 void Student::setStudentData(int *markArray, int &numStudents, int numSub, string fullName) {

marks = new int[numSub];
for (int i = 0; i < numSub; i++) {
marks[i] = markArray[i];
}

this->numOfSubjects = numSub;
this->fullName = fullName;
}

只有在我实现了我的写函数之后,这个问题才会出现:

void writeFile(fstream &fout, char *argv[], Student *pupil, int &numRecs)        {
const char sep = ' ';
const int nameWidth = 5;
const int numWidth = 7;

fout.open(argv[2]);

if (!fout.good()) {
cout << "Error: Invalid data in " << argv[1] << " file." << endl;
cout << "The program is terminated.";
exit(EXIT_FAILURE);
}
else {
// creating the table output
fout << left << setw(nameWidth) << setfill(sep) << "Full Name";
fout << left << setw(numWidth) << setfill(sep) << "mark1";
fout << left << setw(numWidth) << setfill(sep) << "mark2";
fout << left << setw(numWidth) << setfill(sep) << "mark3";
fout << left << setw(numWidth) << setfill(sep) << "mark4";
fout << left << setw(numWidth) << setfill(sep) << "average";
fout << left << setw(numWidth) << setfill(sep) << "min";
fout << left << setw(numWidth) << setfill(sep) << "max";
fout << endl;

for (int i = 0; i < numRecs; i++) { //numRecs being number of records/students
fout << left << setw(nameWidth) << setfill(sep) << pupil[i].getFullName();
for (int j = 0; j < pupil[i].getNumOfSubjTaken(); j++) { //writes each mark up to
//fout << left << setw(numWidth) << setfill(sep) << pupil[i].marks[j];
//This line doesn't work, but i need to be able to write the marks.
}
if (pupil[i].getNumOfSubjTaken() < 4) {
for (int k = pupil[i].getNumOfSubjTaken(); k != 4; k++) {
fout << left << setw(numWidth) << setfill(sep) << " ";
}
}
fout << left << setw(numWidth) << setfill(sep) << pupil[i].getAverageMark();
fout << left << setw(numWidth) << setfill(sep) << pupil[i].getLowestMark();
fout << left << setw(numWidth) << setfill(sep) << pupil[i].getHighestMark();
fout << endl;
}

}
}

我似乎也无法拒绝 << pupil[i].marks[j];尽管它应该有效。

感谢您的宝贵时间和帮助。

最佳答案

您的赋值运算符不正确,因为它只做了 marks 的浅拷贝指针。因此,自 marks 以来,您将收到双重释放错误。当对这些对象调用析构函数时,两个对象(thispupil)中的指针将指向同一内存。

请注意,如果您使用了 std::vector<int> marks;而不是 int *marks; ,那么就不需要复制构造函数、赋值运算符或析构函数,如 std::vector<int>基本上完成您在复制构造函数、赋值运算符和析构函数中尝试执行的操作。不同之处在于 std::vector<int>安全、高效且无差错地执行此操作。

话虽如此,对您的代码进行修复(不是唯一可能的修复)是分配与传入的主题数量相匹配的新内存 Student对象,解除分配 marks内存,然后分配 marks复制数据到新分配的内存。

Student &Student::operator=(const Student &pupil) 
{
if (this != &pupil)
{
// allocate new memory and copy
int *temp = new int [pupil.numOfSubjects];
for (int i = 0; i < pupil.numOfSubjects; i++)
temp[i] = pupil.marks[i];

// deallocate old memory and assign
delete [] marks;
marks = temp;
fullName = pupil.fullName;
numOfSubjects = pupil.numOfSubjects;
}
return *this;
}

作为上述代码的替代方案,因为您似乎有一个有效的复制构造函数和析构函数(以下内容正常工作所必需的),一个更简单的解决方案是使用 copy / swap idiom .

#include <algorithm>
//...
Student &Student::operator=(const Student &pupil)
{
Student temp(pupil);
std::swap(temp.numOfSubjects, numOfSubjects);
std::swap(temp.marks, marks);
std::swap(temp.fullName, fullName);
return *this;
}

这利用复制构造函数和析构函数创建一个临时对象,然后换出 this 的内部结构与临时对象的内部结构。然后临时对象与旧的内部结构一起消失。

关于C++ double free or corruption (out) : Even with copy constructor and assignment operator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39135985/

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