c ++ random cout语句导致程序在重载的内部崩溃' ='操作者

时间:2013-07-31 22:41:06

标签: c++

我不得不把这个程序用于作业,它已经被评分,得到了80分。我要回去清理一些代码了。在我的代码的底部是重载= operator的函数。出于某种原因,我注释掉的cout语句会导致我的程序崩溃,但是,当我删除// s程序运行正常时...

任何人都有任何想法,为什么会这样?

#include <iostream>
using namespace std;

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

    public:
        Poly();
        Poly(int Order);
        Poly(int Order, int * Coeff);
        ~Poly(){/*cout << "Deconstructor\n";*/};
        Poly(const Poly &rhs);

        //accessors &  mutators
        void set();
        void set(int * Coeff, int Order);
        void Print2();
        void PtrReset();
        int getorder(){return order;};
        int * get()const{return coeff;};

        //Overloaded Operators
        Poly operator+(const Poly &rhs);
        Poly operator-(const Poly &rhs);
        Poly operator*(const int scale);
        Poly operator=(const Poly &rhs);
        Poly operator*(const Poly &rhs);
        const int& operator[](int I)const;
        int& operator[](int I);
        bool operator==(const Poly &rhs);
        int operator( )(int X);
        friend ostream & operator<<(ostream & Out, const Poly &rhs);
        friend istream & operator >>(istream & In, Poly &rhs);
};

int main()
{
    int coeff1[ ] =  {-19,1,-12,3,1};
    int coeff2[ ] = {-19,1,-6,0,0,7,0,-1};

    bool flag;

    Poly P1(4, coeff1);
    Poly P2(7, coeff2);
    Poly P3;
    P3 = P1 + P2;
    cout << "P1 + P2: " << P3;
    P3 = P2 - P1;
    cout << "P2 - P1: " << P3;
    P3 = P1 * 10;
    cout << "P1 * 10: " << P3;
    P3 = P1 * P2;
    cout << "P1 * P2: " << P3;
    flag = (P2 == P2);
    cout << "P2 and P2 are ";
    if (flag)
        cout << "Equal\n";
    else
        cout << "Not Equal\n";
    flag = (P1 == P2);
    cout << "P1 and P2 are ";
    if (flag)
        cout << "Equal\n";
    else
        cout << "Not Equal\n";
    cout << "P1(4) = " << P1(4) << endl;
    cout << "P1: ";
    cout << P1;
    cout << "P2: ";
    cout << P2;
    P1[3] = P2[5];
    cout << "P1(after P1[3] = P2[5]): " << P1;
    P1[3] = P2[4];
    cout << "P1(after P1[3] = P2[4]): " << P1;

    return 0;
}

Poly::Poly()
{
    coeff = (int *) malloc(sizeof(int));
    order = 0;
    *coeff = 0;
    //cout << "Default Constructor\n";
}

Poly::Poly(int Order)
{
    order = Order;
    coeff = new int[order+1];
    for(int i(0); i <= order; i++)
        coeff[i] = 0;
}

Poly::Poly(int Order, int * Coeff)
{
    order = Order;
    coeff = Coeff;
}

Poly::Poly(const Poly &rhs)
{
    order = rhs.order;
    int *temp;
    temp = (int *) malloc((rhs.order + 1) * sizeof(int));
    coeff = (int *) malloc((order + 1) * sizeof(int));
    temp = rhs.coeff;
    for(int i(0); i <= order; i++)
    {
        *coeff = *temp;
        coeff++;
        temp++;
    }
    PtrReset();
}


void Poly::set()
{
    coeff = (int *) malloc((order + 1) * sizeof(int));
    if(coeff == NULL)
    {
        cout << "Error allocating memory!\n";
        exit(1);
    }
    cout << "Begin entering with the non x value, then x^1 coefficient, x^2, etc:\n";
    for(int i(0); i <= order; i++)
    {
        cout << (i+1) << ". ";
        cin >> * coeff;
        coeff++;
    }   
    PtrReset();
}

void Poly::set(int * Coeff, int Order)
{
    order = Order;
    for(int i(0); i <= order; i++)
    {
        *coeff = *Coeff;
        coeff++;
        Coeff++;
    }
    PtrReset();
}


void Poly::Print2()
{
    for(int i(0); i <= order; i++)
    {
        cout << * coeff << " ";
        coeff++;
    }
    cout << endl;
    PtrReset();
}

void Poly::PtrReset()
{
    for(int i(0); i <= order; i++)
        coeff--;
}

Poly Poly::operator+(const Poly &rhs)
{
    int neworder;
    int * newpointer;
    int * temp;
    temp = rhs.coeff;
    if(order >= rhs.order)
    {
        neworder = order;
        newpointer = (int *) malloc((neworder+1) * sizeof(int));
        for(int i(0); i <= rhs.order; i++)
        {
            *newpointer = *coeff + *temp;
            newpointer++;
            coeff++;
            temp++;
        }
        for(int i(0); i < (order - rhs.order); i++)
        {
            *newpointer = *coeff;
            newpointer++;
            coeff++;
        }       
    }
    else
    {
        neworder = rhs.order;
        newpointer = (int *) malloc((neworder+1) * sizeof(int));
        for(int i(0); i <= order; i++)
        {
            *newpointer = *coeff + *temp;
            newpointer++;
            coeff++;
            temp++;
        }
        for(int i(0); i < (rhs.order - order); i++)
        {
            *newpointer = *temp;
            newpointer++;
            temp++;
        }       
    }
    PtrReset();
    for(int i(0); i <= neworder; i++)
        newpointer--;

    return Poly(neworder, newpointer);
}

Poly Poly::operator-(const Poly &rhs)
{
    int neworder;
    int * newpointer;
    int * temp;
    temp = rhs.coeff;
    if(order >= rhs.order)
    {
        neworder = order;
        newpointer = (int *) malloc((neworder+1) * sizeof(int));
        for(int i(0); i <= rhs.order; i++)
        {
            *newpointer = *coeff - *temp;
            newpointer++;
            coeff++;
            temp++;
        }
        for(int i(0); i < (order - rhs.order); i++)
        {
            *newpointer = *coeff;
            newpointer++;
            coeff++;
        }       
    }
    else
    {
        neworder = rhs.order;
        newpointer = (int *) malloc((neworder+1) * sizeof(int));
        for(int i(0); i <= order; i++)
        {
            *newpointer = *coeff - *temp;
            newpointer++;
            coeff++;
            temp++;
        }
        for(int i(0); i < (rhs.order - order); i++)
        {
            *newpointer = 0 - *temp;
            newpointer++;
            temp++;
        }       
    }
    PtrReset();
    for(int i(0); i <= neworder; i++)
        newpointer--;

    return Poly(neworder, newpointer);
}

Poly Poly::operator*(const int scale)
{
    int neworder = order;
    int * temp = new int[order+1];

    for(int i(0); i <= order; i++)
        temp[i] = scale * coeff[i];

    return Poly(neworder, temp);
}
Poly Poly::operator*(const Poly &rhs)
{
    int neworder = order + rhs.order;
    int * newcoeff;
    int * temp;
    temp = rhs.coeff;
    newcoeff = (int *) malloc((neworder + 1) * sizeof(int));
    for(int i(0); i <= neworder; i++)
    {
        *newcoeff = 0;
        newcoeff++;
    }
    for(int i(0); i <= neworder; i++)
        newcoeff--;

    for(int i(0); i <= order; i++)
    {
        for(int j(0); j <= rhs.order; j++)
        {
            *newcoeff += (*coeff * *temp);
            newcoeff++;
            temp++;
        }
        coeff++;
        for(int j(0); j < rhs.order; j++)
            newcoeff--;
        for(int j(0); j <= rhs.order; j++)
            temp--;
    }
    for(int i(0); i <= (neworder - rhs.order); i++)
        newcoeff--;
    PtrReset();

    return Poly(neworder, newcoeff);
}
bool Poly::operator==(const Poly &rhs)
{
    bool test = true;
    int count = 0;
    while(test && (count <= order))
    {
        if(order != rhs.order)
            test = false;
        else if(coeff[count] != rhs.coeff[count])
            test = false;
        count++;
    }


    return test;
}

int Poly::operator()(int X)
{
    int * temp;
    int * temp2;
    temp = (int *) malloc((order + 1) * sizeof(int));
    temp2 = temp;
    for(int i(0); i < order; i++)
        coeff++;
    for(int i(0); i <= order; i++)
    {
        *temp = *coeff;
        temp++;
        coeff--;
    }
    coeff++;
    for(int i(0); i < order; i++)
        temp--;
    for(int i(0); i < order; i++)
    {
        *temp += (X * *temp2);
        temp++;
        *temp2++;
    }
    return *temp2;
}

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;
} 
int& Poly::operator[](int I)
{
    if(I > (order + 1) || I < 0)
    {
        cout << "Request to access outside Poly boundaries!" << endl;
        exit(1);
    }
    return coeff[I];
}
const int& Poly::operator[](int I) const
{
    if(I > (order + 1) || I < 0)
    {
        cout << "Request to access outside Poly boundaries!" << endl;
        exit(1);
    }

    return coeff[I];
}
Poly Poly::operator=(const Poly &rhs)
{
    order = rhs.order;

    //cout << "new order = " << rhs.order << endl;  ******HERE*******

    for(int i(0); i <= rhs.order; i++)
        coeff[i] = rhs.coeff[i];
    return Poly(order, coeff);
}

2 个答案:

答案 0 :(得分:2)

Assignment operator作为会员应返回对this的引用 如果不这样做,那么您可能不应该使用assignment operator

除了注释外,您的赋值运算符的另一个直接问题是,当您手动构建新的coeff数组时,您正在泄漏更多内存。您还将返回第三个对象,这不是任何使用assignment operator的人所期望的,这是彻头彻尾的危险

Poly& Poly::operator=(const Poly &rhs)
{
    order = rhs.order;
    // you should probably initialize coeff here

    //cout << "new order = " << rhs.order << endl;  ******HERE*******

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

    //This is wrong return this. see signature fix above
    //return Poly(order, coeff);
    return *this;

}

其次,不要手动纠正你在代数运算符中所做的任何事情,复制构造FIRST然后修改THAT对象。

例如乘法:

//member multiplication which still sucks because we are going to copy construct again to return..
Poly Poly::operator*(const int scale)
{
    Poly result(*this);
    // now manipulate your new object and leave THIS this alone

    for(int i(0); i <= result.order; i++)
        result.coeff[i] *= scale;

    return result;
}

如果你把它变成一个非成员函数,然后创建一个成员函数*=operator,然后你可以像Ben和我想象的那样做

,那就更好了
Poly operator*(Poly result, int scale)
{
    return (result *= scale);
}

我看到的主要问题是你正在疯狂地泄露记忆,并且从未对它有任何限制。

答案 1 :(得分:0)

当我看到这个时,它背后的常见问题是双重免费/删除。你可能会在相同的指针上自由地调用两次(或者一个无效的指针),这会导致一切变得奇怪,并且崩溃几乎可以在任何时候发生。

当你混合malloc和new时也会发生这种情况。将所有内容切换为新内容,看看你是否仍然遇到了崩溃。

相关问题