my code is producing * Error in `./a.out': double free or corruption (out): 0x00007ffe400eb0e0 *
Whenever it's run, i assumed this was an issue based on either my copy constructor or how I delete my dynamic array but i'm having trouble working out where the issue arises:
My 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;
};
Copy Constructor:
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;
}
Assignment Operator:
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;
}
Deconstructor:
Student::~Student(){
if (marks != NULL) {
delete [] marks;
}
marks = NULL;
numOfSubjects = 0;
fullName = "";
}
Set Function (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;
}
And it was only after i implemented my write function that this issue was occurring:
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;
}
}
}
I also can't seem to fout << pupil[i].marks[j]; even though it should work.
Thanks for your time and help.