【问题标题】:Class Pointer Memory Issue类指针内存问题
【发布时间】:2013-11-22 17:33:47
【问题描述】:

我已经把它修整了,所以问题仍然存在。我将能够摆脱为我的作业创建的内容,但是,内存管理导致了一些问题。如果我在复制代码时运行此代码,它会在转到重载的

根据What is the right way to allocate memory in the C++ constructor?,我相信我已经正确完成了内存分配。我不知道错误来自哪里。

#include <iostream>
#include <iomanip>
using namespace std;

class Poly
{
private:
int order; //order of the polynomial
int size; //order + 1
int * coeff;//pointer to array of coeff on the heap

public:
    Poly();
    Poly(int Order);
    Poly(int Order, int * Coeff);
    ~Poly(){delete [] coeff; cout << "Destructor\n";};
    Poly(const Poly &rhs);

//accessors &  mutators
    void set(int * Coeff, int Order);

//Overloaded Operators
    Poly operator+(const Poly &rhs);
    Poly & operator=(const Poly &rhs);
    friend ostream & operator<<(ostream & Out, const Poly &rhs);
};

int main()
{
    int coeff1[ ] =  {-38,2,-24,6,4};
    int coeff2[ ] = {-38,2,-14,0,0,10,0,4};
    int size;

    bool flag;

    Poly P1(4, coeff1);
    Poly P2(7, coeff2);
    Poly P3;
    P3 = P1 + P2;
    cout << "P1 + P2: " << P3;

    return 0;
}
Poly::Poly()
{
    order = 0;
    size = order + 1;
    coeff = new int[size];
    coeff[0] = 0;
    cout << "Default Constructor\n";
}
Poly::Poly(int Order)
{
    order = Order;
    size = order + 1;
    coeff = new int[size];
    for(int i(0); i <= order; i++)
        coeff[i] = 0;
    cout << "Order Constructor\n";
}
Poly::Poly(int Order, int * Coeff)
{
    order = Order;
    size = order + 1;
    coeff = new int[size];
    for(int i(0); i <= order; i++)
        coeff[i] = Coeff[i];
    cout << "Complete Constructor\n";
}
Poly::Poly(const Poly &rhs)
{
    order = rhs.order;
    size = rhs.size;
    int *coeff = new int[size];
    for(int i(0); i <= order; i++)
        coeff[i] = rhs.coeff[i];
    cout << "Copy Constructor\n";
}
void Poly::set(int * Coeff, int Order)
{
    order = Order;
    size = order + 1;
    for(int i(0); i <= order; i++)
        coeff[i] = Coeff[i];
}
Poly Poly::operator+(const Poly &rhs)
{
    int neworder = max(order, rhs.order);
    int * newcoeff = new int[neworder+1];
    Poly temp(neworder, newcoeff);
    delete [] newcoeff;

    for(int i(0); i <= temp.order; i++)
        temp.coeff[i] = 0;
    for(int i(0); i <= order; i++)
        temp.coeff[i] = coeff[i];
    for(int i(0); i <= rhs.order; i++)
        temp.coeff[i] += rhs.coeff[i];

    return Poly(temp.order, temp.coeff);
}

ostream &operator <<(ostream& out, const Poly &source)
{
    for(int i(source.order); i >= 0; i--)
    {
        if(i == 1)
        {
            if(i == source.order)
                if(source.coeff[i] == 1)
                    out << "X";
                else if(source.coeff[i] == -1)
                    out << "-X";
                else
                    out << source.coeff[i] << "X";
            else if(source.coeff[i] == 1)
                out << " + " << "X";
            else if(source.coeff[i] == -1)
                out << " - " << "X";
            else if(source.coeff[i] > 0)
                out << " + " << source.coeff[i] << "X";
            else if(source.coeff[i] < 0)
                out << " - " << abs(source.coeff[i]) << "X";
        }

        else if(i > 1)
        {   
            if(i == source.order)
                if(source.coeff[i] == 1)
                    out << "X^" << i;
                else if(source.coeff[i] == -1)
                    out << "-X^" << i;
                else
                    out << source.coeff[i] << "X^" << i;
            else if(source.coeff[i] == 1)
                out << " + " << "X^" << i;
            else if(source.coeff[i] == -1)
                out << " - " << "X^" << i;
            else if(source.coeff[i] > 1)
                out << " + " << source.coeff[i] << "X^" << i;
            else if(source.coeff[i] < -1)
                out << " - " << abs(source.coeff[i]) << "X^" << i;
        }
        else
        {
            if(source.coeff[i] > 0)
                out << " + " << source.coeff[i];
            else if(source.coeff[i] < 0)
                out << " - " << abs(source.coeff[i]);
        }
    }   
    out << endl;

   return out;
} 
Poly & Poly::operator=(const Poly &rhs)
{
    order = rhs.order;  
    for(int i(0); i <= rhs.order; i++)
        coeff[i] = rhs.coeff[i];

    return *this;
}

【问题讨论】:

  • 您可以删除多少这段代码,但仍然会产生错误?这将帮助您解决问题。 sscce.org
  • @Adam Liss 我已缩短代码以“隔离”问题。我还从另一个用户那里获取了一些 cmets 并将它们应用于重载的 + 运算符。
  • 你的operator= 做坏事如果this-&gt;order &lt; rhs.order...
  • @aschepler 你说得对。那是我记忆犹新的地方。它似乎解决了我的问题。非常感谢!

标签: c++ pointers memory-management new-operator


【解决方案1】:

呃...这看起来是个坏主意:

Poly Poly::operator*(const Poly &rhs)
{
    Poly temp;
    //...

    temp.set(newcoeff, neworder);

这看起来很糟糕,因为 Poly temp 设置了一个 0 阶多项式,但 Poly::set 不会将 coeff 重新分配给 neworder 指定的新大小。

Poly::set 应该 delete [] coeff 并将其重新分配到新订单指定的新大小。

我没有看其余的代码,看看是否还有其他类似的错误,但这个错误立即跳出来。

要获得更强大的解决方案,您应该考虑将int * 替换为vector&lt;int&gt;,并让编译器和运行时为您管理内存。您甚至不需要跟踪大小,因为vector&lt;&gt;::size() 会自动为您完成。

【讨论】:

  • +1 你打败了我。我刚刚发现了。 [] 运算符中还有一个小的非一错误,它测试 &gt; size 而不是 &gt;= size(或 &gt; order)。
  • @Joe Z,我希望我可以使用向量,但我们不允许执行此任务。根据您的评论,我重新设计了 + 运算符,因为我正在处理与 * 运算符相同的内存。我仍然有问题。
  • 你返工了吗set()?这就是主要问题所在。它应该根据新的大小更新ordersize 并重新分配(即delete[] 后跟new[]coeff,然后再复制到新的系数中。
  • 您的operator=set() 有类似的问题。它应该做几乎相同的事情。
  • @JoeZ 是的,我修改了它们。我花了一段时间才意识到发生了什么,但我终于明白了。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-16
  • 1970-01-01
  • 1970-01-01
  • 2020-12-16
  • 1970-01-01
相关资源
最近更新 更多