【发布时间】:2023-03-07 23:59:01
【问题描述】:
我的代码正在生成 * `./a.out' 中的错误:双重释放或损坏(输出):0x00007ffe400eb0e0 *
每当它运行时,我都认为这是一个基于我的复制构造函数或我如何删除动态数组的问题,但我无法确定问题出在哪里:
我的班级:
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 = "";
}
设置函数(Mutator):
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;
}
}
}
我也看不出来
感谢您的时间和帮助。
【问题讨论】:
-
析构函数在哪里?此外,您的赋值运算符存在内存泄漏,因为您未能释放以前的内存(并重新分配新内存)。但是,当您可以简单地执行 copy / swap 时,为什么所有这些都可以在赋值运算符中工作?
-
另外,你的析构函数可以是
Student::~Student() {delete [] marks;}。无需检查 NULL,将值分配给正在被销毁的对象的成员是没有意义的。 -
赋值运算符中的
marks = pupil.marks;是错误的。setStudentData泄漏任何旧数据,并且不设置numOfSubjects.,并且不使用它的 2 个参数
标签: c++ class copy-constructor assignment-operator