gpt4 book ai didi

c++ - 类指针内存问题

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

我已经减少了它,所以问题仍然存在。我将能够摆脱为我的任务创建的内容,但是,内存管理导致了一些问题。如果我按复制的方式运行这段代码,它会在转到重载的 << 函数时挂起。

根据 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;
}

最佳答案

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

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

temp.set(newcoeff, neworder);

Poly temp 以来,它看起来坏了设置一个 0 阶多项式,但是 Poly::set不重新分配 coeffneworder 指定的新尺寸.

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

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

要获得更强大的解决方案,您应该考虑更换 int *vector<int> ,并让编译器和运行时为您管理内存。您甚至不需要跟踪大小,因为 vector<>::size()会自动为你做那件事。

关于c++ - 类指针内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19897494/

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