I am writing a polynomial class and finally got done writing all the functions, set up the main but get an assertion failed _BLOCK_TYPE_IS_VALID(pHead->nBlockUse). I read it has something to do with the going over the size of the heap, but don't entirely grasp that. Any clues?

#include <iostream>
#include "polynomial.h"

using namespace std;

int main()
{
	int c1[5] = {-19, 1, -12, 3, 2};
	int c2[8] = {-19, 1, -6, 0, 0, 7, 0, 2};

	Poly p1(4, c1);
	Poly p2(7, c2);
	Poly p3;

	p3 = p1 + p2;
	cout << p3;

	p3 = p1 - p2;
	cout << p3;

	p3 = p1 * 10;
	cout << p3;

	p3 = p1 * p2;
	cout << p3;

	bool flag = p1 == p2;
	cout << "Are p1 and p2 equal? (0 = false, 1 = true) " << flag << endl;

	p1[3] = p2[5];
	cout << p1;

	return 0;
}


#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

#include <iostream>
#include <cmath>

using namespace std;

class Poly
{
private:
	int order;
	int size;
	int *coeff;

public:
	//Default Constructor
	Poly():order(1)
	{
		coeff = new int[1];
		*(coeff + 0) = 1;
	}
	//Parameterized Constructor
	Poly(int Order, int Default):order(Order)
	{
		size = order + 1;
		coeff = new int[size];
		for(int i = 0; i < size; i++)
			*(coeff + i) = Default;
	}
	//Use array to assign coefficients
	Poly(int Order, int *Coeff):order(Order)
	{
		size = order + 1;
		coeff = new int[size];
		for(int i = 0; i < size; i++)
			*(coeff + i) = Coeff[i];
	}
	//Copy constructor
	Poly(const Poly &rhs)
	{
		size = rhs.size;
		coeff = new int[size];
		memcpy(coeff, rhs.coeff, sizeof(int)*size);
	}
	//Destructor
	~Poly(){};

	void set()
	{
		cout << endl;
		for(int i = 0; i < size; i++)
		{
			cout << "Enter X^" << i << ": ";
			cin >> *(coeff + i);
			
		}
		cout << endl;
	}
	void set(int Coeff[], int Size)
	{
		delete []coeff;
		order = Size - 1;
		coeff = new int[Size];
		coeff = Coeff;
	};
	int getOrder(){return order;}
	int * get(){return coeff;}

	Poly operator+(const Poly &rhs)
	{
		int sizen;
		int *Coeff;
		if(size < rhs.size)
			sizen = rhs.size;
		else
			sizen = size;
		Coeff = new int[sizen];
		for(int i = 0; i < sizen; i++)
			Coeff[i] = *(coeff + i) + *(rhs.coeff + i);
		return Poly(sizen - 1, Coeff);
	}

	Poly operator-(const Poly &rhs)
	{
		int sizen;
		int *Coeff;
		if(size < rhs.size)
			sizen = rhs.size;
		else
			sizen = size;
		Coeff = new int[sizen];
		for(int i = 0; i < sizen; i++)
			Coeff[i] = *(coeff + i) - *(rhs.coeff + i);
		return Poly(sizen - 1, Coeff);
	}

	Poly operator*(const int scale)
	{
		int *Coeff;
		Coeff = new int[size];
		for(int i = 0; i < size; i++)
			Coeff[i] = *(coeff + i) * scale;
		return Poly(size - 1, Coeff);
	}

	Poly operator*(const Poly &rhs)
	{
		int *Coeff;
		int Order = order + rhs.order;
		Coeff = new int[Order + 1]();
		for(int i = 0; i < size; i++)
		{
			for(int j = 0; j < rhs.size; j++)
			{
				*(Coeff + (i + j)) += coeff[i] * rhs.coeff[j];
			}
		}
		return Poly(Order, Coeff);
	} 
	
	bool operator==(const Poly &rhs)
	{
		bool test = true;
		if(size == rhs.size)
		{
			for(int i = 0; i < size; i++)
			{
				if(*(coeff + i) != *(rhs.coeff + i))
					test = false;
			}
		}
		else
			test = false;
		return test;
	}
	
	Poly operator=(const Poly &rhs)
	{
		delete []coeff;
		return Poly(rhs.size - 1, rhs.coeff);
	}

	const int & operator[](int I)const{return *(coeff + I);}
	int & operator[](int I){return *(coeff + I);}

	int operator()(int x)
	{
		int y = 0;
		for(int i = 0; i < size; i++)
			y += coeff[i] * pow(static_cast<double>(x), i);
		return y;
	}

	friend ostream &operator<<(ostream &Out, const Poly &rhs)
	{
		for(int i = (rhs.size - 1); i > 1; i--)
		{
			if(i == rhs.size - 1)
			{
				if(rhs.coeff[i] > 0)
					Out << rhs.coeff[i] << "X^" << i;
				else 
					if(rhs.coeff[i] < 0)
						Out << "-" << (rhs.coeff[i] * -1) << "X^" << i;
			}
			else if(rhs.coeff[i] > 0)
				Out << " + " << rhs.coeff[i] << "X^" << i;
			else if(rhs.coeff[i] < 0)
				Out << " - " << (rhs.coeff[i] * -1) << "X^" << i;
		}
		if(rhs.coeff[1] > 0)
			Out << " + " << rhs.coeff[1] << "X";
		else if(rhs.coeff[0] < 0)
			Out << " - " << (rhs.coeff[1] * -1) << "X";
		if(rhs.coeff[0] > 0)
			Out << " + " << rhs.coeff[0];
		else if(rhs.coeff[0] < 0)
			Out << " - " << (rhs.coeff[0] * -1);
		Out << endl;

		return Out;
	}

	friend istream &operator>>(istream &In, Poly &rhs)
	{
		cout << "Enter order of polynomial: ";
		In >> rhs.order;
		rhs.size = rhs.order + 1;
		for(int i = 0; i < rhs.size; i++)
		{
			cout << "Enter coefficient to X^" << i << " : ";
			In >> *(rhs.coeff + i);
		}
		return In;
	}

};

#endif

To answer my own question, the problem is in the assignment operator. I delete the array without reallocating memory for it before I try and return the array.

This article has been dead for over six months. Start a new discussion instead.