我的代码是生产 *错误的`./a.out“:双重释放或腐败(出):0x00007ffe400eb0e0 *C++双重释放或腐败(出):即使有拷贝构造函数和赋值运算符
每当它的运行,我认为这是基于要么我的拷贝构造函数的问题,或者我如何删除我的动态数组,但我无法工作哪里出了问题就出现了:
我的班级:
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;
}
}
}
我也似乎无法FOUT < <瞳孔[I] .marks [J]。即使它应该工作。
感谢您的时间和帮助。
析构函数在哪里?另外,由于您无法取消分配先前的内存(并重新分配新内存),您的赋值运算符会发生内存泄漏。但是,为什么所有这些在赋值运算符中工作时,只需执行[copy/swap](http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom)? – PaulMcKenzie
另外,你的析构函数可以简单的是'Student ::〜Student(){delete [] marks;}'。没有必要检查NULL,将值分配给被销毁的对象的成员是没有意义的。 – PaulMcKenzie
'marks = pupil.marks;'在赋值运算符中是错误的。 'setStudentData'泄漏任何旧数据,并且不设置'numOfSubjects',并且不使用其参数的2个 –