- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我的代码正在生成* `./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
以来,您将收到双重释放错误。当对这些对象调用析构函数时,两个对象(this
和 pupil
)中的指针将指向同一内存。
请注意,如果您使用了 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/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎离题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a minim
家庭作业 必须同时重载 operator>(istream &in, Complex &value); public: // constructor Compl
这个问题在这里已经有了答案: What are the basic rules and idioms for operator overloading? (8 个答案) 关闭 5 年前。 有什么区别
这是文档中一个不清楚的示例,使用此运算符:http://package.elm-lang.org/packages/elm-lang/core/3.0.0/Json-Decode#at 最佳答案 请注
我不明白这种行为: > sort([1,2,3,4]) ~~ sort([1,2,3,4]) False 你能给我解释一下吗? 为什么这两个列表(显然是相等的)根据 Perl 6 不相等。 更新 有趣
我正在尝试将 Ø 设为逻辑否定运算符。 ¬ True; multi sub prefix: ($n) { return not $n; } 当我运行上面的程序时,它返回以下错误: $
class Port { private: char * brand; char style[20]; // i.e., tawny, ruby, vintage int bo
早上好。我有一些问题。我有这些字段: name: "Mike", city: "NY", address: "something", pets: ["dog", "cat"] 我创建了索引 db.pe
我有以下代码使用 Javascript Webcrypto-API 解密 AES 加密数据,但它会导致“OperationError”并显示消息“操作因操作特定原因而失败”: function loa
我制作了一个自定义 Airflow 操作符,这个操作符接受一个输入,这个操作符的输出在 XCOM 上。 我想要实现的是使用一些定义的输入调用运算符,将输出解析为可在分支运算符内部调用的 Python,
int a = 1; a += ++a; cout << a << endl; // 4 int a = 1; a += a++; cout << a << endl; // 3 为什么这两个例子有不
我有这个队列的实现: #include using namespace std; template struct elem_q { T inf; elem_q* link; }; template
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我对 C++ 中的运算符重载有疑问。我有代表复数的结构,我正在重载运算符,所以我可以用复数进行计算。Visual Studio 2012 给我这个错误:1 IntelliSense:没有运算符“!=”
我正在尝试创建一个 BoolArray 类,它表示一个 bool 值数组,而不是为每个 bool 变量保存 1 个 bool 值。它使用 1 个字符变量来表示使用位的 8 个 bool 值,并节省内存
我类有 2 个运算符有点问题。 我的类(class)宣布: template class MyMap{ keyType keys[MAX]; valueType values[MAX
我有类的迭代器模板和用于 for 语句的类。 template class Itr2 { public: Itr2() { } ~Itr2() { } typedef t
Section 7.2 Enumeration declarations 没有说明任何关于 operator!=() 和作用域的 operator==()枚举。但是下面的代码可以编译。 #includ
我需要将一个 int 序列化到本地文件并将其读入内存。这是代码 #include "stdafx.h" #include #include using namespace std; int _tm
operator += 这样定义对吗?! void operator +=(const BigNumber& other) { *this=(*this) + other; } 在这样的类中:
我是一名优秀的程序员,十分优秀!