I'm writing a class which deals with polynomials of an arbitrary size, and when overloading the * operator I have a buffer overrun warning that I don't know how to deal with.
Polynomial Polynomial::operator*(const Polynomial& rhs)
{
int returnSize = (this->size + rhs.getSize()) - 1;
int* returnArray = new int[returnSize];
for (int i = 0; i < this->size; i++)
{
for (int j = 0; j < rhs.getSize(); j++)
{
returnArray[i + j] = this->polynomial[i] * rhs.polynomial[j];
}
}
}
And I think that by the nature of the program that it isn't actually possible to get buffer overrun, but I don't understand it very well so I'm probably mistaken. The warning states:
C6386: Buffer Overrun whilst writing to 'returnArray': the writable size is 'returnSize*4' bytes, but'8' bytes might be written.
I'm also having this issue with the copy constructor.
Polynomial::Polynomial(const Polynomial& rhs)
{
this->size = rhs.getSize();
this->polynomial = new int[rhs.getSize()];
for (int i = 0; i < rhs.getSize(); i++)
{
this->polynomial[i] = rhs.polynomial[i];
}
}
I really hope this isn't a daft question, and thanks for your help!
(this->size + rhs.getSize()) - 1;why do you subtract 1? And I think your indexing[i + j]is incorrect. - vahanchostd::vector<int>rather than new'ing up your own int [] every time. - Tanveer Badar