主语句中的返回语句上的堆损坏

Heap corruption on return statement in Main

本文关键字:语句 损坏 返回      更新时间:2023-10-16

我正在创建一个保存多项式的动态数组类。我现在遇到的问题是当我运行我的代码时,一旦它命中 main 中的 return 语句,它就会开始调用析构函数并开始从从 C 开始的每个实例中释放内存。它删除C很好,但是当它到达B时,我收到堆损坏错误。我尝试遍历代码,但我看不到损坏发生的位置。谁能帮我?它给我的确切错误是"CRT 检测到应用程序在堆缓冲区结束后写入内存"。

*编辑:我非常乐意得到人们的建议,以帮助我的代码更好,但请记住,这是针对一个类并且有特定的规则。我不能使用STL的任何东西。我喜欢你能给我的任何批评。
///////////////////////////页眉/////////////////////////////

class Poly
{
friend std::ostream& operator<<(std::ostream& output, const Poly& pNomial);
public:
Poly();
Poly(const int& coeff, const int& degree = 0);
Poly(const Poly& copy);
~Poly();

void setCoeff(const int& coeff, const int& degree);     
bool isEmpty()const;
Poly& operator=(const Poly& pNomial);
private:
int* coeffs;
int highestDegree;

};

///////////////////////////.CPP////////////////////////

#include "poly.h"
Poly::Poly()
{
highestDegree = 0;
coeffs = new int[highestDegree+1]();
}
Poly::Poly(const int & coeff, const int & degree)
{
if (degree >= 0)
{
highestDegree = degree;
coeffs = new int[highestDegree + 1]();
coeffs[degree] = coeff;
}
else
{
highestDegree = 0;
coeffs = new int[highestDegree + 1]();
}

}
Poly::Poly(const Poly& copy)
{
highestDegree = copy.highestDegree;
coeffs = new int[highestDegree + 1]();
for (int i = 0; i < copy.highestDegree + 1; i++)
{
coeffs[i] = copy.coeffs[i];
}
}
Poly::~Poly()
{
delete[] coeffs;
}
void Poly::setCoeff(const int& coeff, const int& degree)
{
if (degree > this->highestDegree)
{
Poly temp = *this;
delete[] this->coeffs;
this->highestDegree = degree;
this->coeffs = new int[highestDegree]();

for (int i = 0; i < temp.highestDegree + 1; i++)
{
this->coeffs[i] = temp.coeffs[i];
}       
}
if (degree >= 0)
{
this->coeffs[degree] = coeff;
}
}
bool Poly::isEmpty()const
{
bool check = true;
for (int i = 0; i < highestDegree + 1 && check; i++)
{
if (coeffs[i] != 0)
{
check = false;
}
}
return check;
}
Poly & Poly::operator=(const Poly& pNomial)
{
if (this != &pNomial)
{
delete[] this->coeffs;
this->highestDegree = pNomial.highestDegree;
this->coeffs = new int[this->highestDegree + 1]();
for (int i = 0; i < pNomial.highestDegree + 1; i++)
{
this->coeffs[i] = pNomial.coeffs[i];
}
}
return *this;
}
std::ostream& operator<<(std::ostream& output, const Poly& poly)
{
if (!poly.isEmpty())
{
for (int i = poly.highestDegree; i >= 0; i--)
{
if (i == 1 && poly.coeffs[i] != 0)
{
if (poly.coeffs[i] >= 1)
{
output << " +" << poly.coeffs[i] << "x";
}
else
{
output << " " << poly.coeffs[i] << "x";
}
}
else if (i == 0 && poly.coeffs[i] != 0)
{
if (poly.coeffs[i] >= 1)
{
output << " +" << poly.coeffs[i];
}
else
{
output << " " << poly.coeffs[i];
}
}
else if (poly.coeffs[i] != 0)
{
if (poly.coeffs[i] >= 1)
{
output << " +" << poly.coeffs[i] << "x^" << i;
}
else
{
output << " " << poly.coeffs[i] << "x^" << i;
}
}
}
}
else
{
output << " 0";
}

return output;
}``

/////////////////////////////////主要/////////////////////////

#include "poly.h"
#include <iostream>
int main()
{
Poly A, B(5, 7), C(2);
B.setCoeff(2, 10);
B.setCoeff(1, 3);
B.setCoeff(5, 4);

std::cout << A << std::endl;
std::cout << B << std::endl;
std::cout << C << std::endl;


return 0;
}

我必须说,我同意评论,你应该认真研究你在Poly类中使用的资源的适当生命周期管理。 要回答您现在面临的问题,请查看setCoeff()功能。

this->coeffs = new int[highestDegree]();

应改为,

this->coeffs = new int[highestDegree + 1]();

使用当前的实现,您可以使用highestDegree分配数组,并在for循环中访问temp.coeffs[highestDegree]这是越界访问,即您循环直到i < temp.highestDegree + 1