gpt4 book ai didi

c++ - 如何防止局部变量被破坏两次?堆损坏

转载 作者:行者123 更新时间:2023-11-28 07:39:40 24 4
gpt4 key购买 nike

我的家庭作业一直有问题,关于创建多项式类并重载一些运算符以根据类工作。我已经完成了大部分工作,但我的析构函数或函数似乎有问题。

问题是,根据我的调试(如果我做对了,我想我做到了),当与我的复制构造函数一起使用时,我的 + 运算符函数 get 的返回值被破坏了两次,如下所示:

//polynomials p1 and p2 are declared and given values beforehand
Polynomial p5=Polynomial();
p5=p1+p2;

这会导致堆损坏错误。

这是我的标题代码:

#ifndef POLYNOMIAL_H_
#define POLYNOMIAL_H_
#include <iostream>

using namespace std;

class Polynomial
{
public:
Polynomial();
Polynomial(int);
~Polynomial();
Polynomial(const Polynomial &);
int getOrder() const;
double getCoefficient(int) const;
void setOrder(int);
void setCoefficient(int,double);
const Polynomial &operator=(const Polynomial &);
const bool &operator==(const Polynomial &);
const double &operator()(double &);
const bool &operator!=(const Polynomial &);

friend Polynomial operator+(const Polynomial &poly1, const Polynomial &poly2);
friend Polynomial &operator+=(Polynomial &poly1,const Polynomial &poly2);
friend Polynomial operator-(const Polynomial &poly1, const Polynomial &poly2);
friend Polynomial &operator-=( Polynomial &poly1,const Polynomial &poly2);
friend Polynomial operator*(Polynomial poly1,double num);
private:
int order;
double *coefficient;
};


#endif

这是我的重载 + 函数,它不是很漂亮,但我的问题不是计算,而是内存。我在类中将其声明为友元函数,根据我的作业规则,我需要在 main.cpp 文件中将其作为自由函数而不是成员函数来实现。

   Polynomial operator+(const Polynomial &poly1, const Polynomial &poly2) //the overloaded +operator. makes the order of the result the bigger order and adds the coefficients for all the orders. returns the result. 
{
Polynomial result;


if(poly1.order >= poly2.order)
{
result.setOrder(poly1.order);
for(int i=poly1.order;i>poly2.order;i--)
{
result.setCoefficient(poly1.order-i, poly1.coefficient[poly1.order-i]);
}
for (int i =poly2.getOrder(); i>=0;i--)
{
result.setCoefficient(poly1.order-i,poly1.coefficient[poly1.order-i]+poly2.coefficient[poly2.order-i]);
}
}
else
{
result.setOrder(poly2.order);

for(int i=poly2.order;i>poly1.order;i--)
{
result.setCoefficient(poly2.order-i, poly2.coefficient[poly2.order-i]);
}
for (int i =poly1.order; i>=0;i--)
{
result.setCoefficient(poly2.order-i,poly1.coefficient[poly1.order-i]+poly2.coefficient[poly2.order-i]);
}
}
return result;
}

我们还需要重载 = 运算符,如果需要,这里就是该函数。

    const Polynomial &Polynomial::operator=(const Polynomial &poly)
{
if(this!=&poly)
{
if(order==poly.order)
{
for(int i=0;i<=order;i++)
{
coefficient[i]=poly.coefficient[i];
}
}
else
{
coefficient=new double[poly.order];
order=poly.order;
for(int i=0;i<=order;i++)
{
coefficient[i]=poly.coefficient[i];
}
}
}
return *this;
}

请记住,我真的是编码和 C++ 的初学者,非常感谢您提供的任何帮助。

编辑:添加深拷贝构造函数。

 Polynomial::Polynomial(const Polynomial &copy) //deep copy constructor
{
order=copy.order;
coefficient=new double[copy.order];
for (int i=0;i<=order;i++)
{
coefficient[i]=copy.coefficient[i];
}
}

最佳答案

您为 poly.order double 分配内存,但在您的 for 循环中,最后一个索引将是 poly.order 而它应该是 poly.order-1 将 <= 更改为 < 将解决该问题。

   coefficient=new double[poly.order];//This is memory leak previous coefficient needs to be deleted
order=poly.order;
for(int i=0;i<=order;i++)//<= will lead to overflow and heap corruption
{
coefficient[i]=poly.coefficient[i];
}

关于c++ - 如何防止局部变量被破坏两次?堆损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16104447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com