0

I've just started learning CPP, please keep that in mind.

Okay so, I am trying to define my own mathematical library, and in it I'm trying to implement a polynomial class.

#include <cmath>
#include <iostream>
class Polynomial
{
public:
    Polynomial(int ord);
    ~Polynomial();
    double* getCoeff();  // returns coefficient array
    double valueAt(double xVal); // returns the value of the polynomail at xVal
    void setCoeff(); // basic "cin" way to add data
    void setCoeff(double* inpCoeff, int ord); // adding data by making a double array first

    double minAt(double low, double high);
    double minVal(double low, double high);
    double maxVal(double low, double high);
    double maxAt(double low, double high);

    Polynomial derivative();  // returns another polynomial which is a derivative of the previous one
    double derivativeAt(double xVal); // value of dervative at a particular value

private:
    double* coeff; // the coefficient array, (a_0 + a_1 x + a_2 x^2 +...)  this arrat contains all the a_i's
    int order; // order of the polynomial
};

Now my int main() function is

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

int main()
{
    std::cout << "Vigilant starting..." << std::endl;
    Polynomial poly(2);
    double arr[] = {
        1.0, 2.0, 1.0
    };
    poly.setCoeff(arr, 2);
    std::cout << poly.getCoeff() << std::endl;
    
    std::cout << "Value of polynomial at 1 is" << poly.valueAt(1) << std::endl;
    std::cout << "Value of polynomial at 1 is" << poly.valueAt(2) << std::endl;

    std::cout << "Value of derivative at 1 is" << poly.derivative().valueAt(1) << std::endl;
    std::cout << "Value of derivative at 1 is" << poly.derivative().valueAt(2) << std::endl;


    return 0;
}

My expected outcome should be successful execution. The program compiles but ends up spitting out an error after successful execution.

The thread 0x4a8 has exited with code 0 (0x0).
HEAP[Vigilant.exe]: Invalid address specified to RtlValidateHeap( 000001D072020000, 000001D0720330C0 )
Vigilant.exe has triggered a breakpoint.

I have no idea what I did wrong. But I'll attach all the definitions used in the int main().

Polynomial::Polynomial(int ord)
{
    order = ord;
    int rOrd = order + 1;
    coeff = new double[rOrd];
}

Polynomial::~Polynomial()
{
    delete[] coeff;
}
void Polynomial::setCoeff(double* inpCoeff, int ord)
{
    order = ord;
    for (int i = 0; i < ord + 1; i++)
    {
        *(coeff + i) = *(inpCoeff + i); 
    }
}
double Polynomial::valueAt(double xVal)
{
    double res = 0.0;
    for (int i = 0; i < order + 1; i++)
    {
        res += pow(xVal, i) * *(coeff + i);
    }
    return res;
}
Polynomial Polynomial::derivative()
{
    Polynomial poly(order - 1);
    poly.order = order - 1;
    for (int i = 0; i < order; i++)
    {
        *(poly.coeff + i) = *(coeff + i + 1) * (i+1.0);
    }
    return poly;
}
  • https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems – πάντα ῥεῖ Jan 16 '21 at 16:33
  • Tip: Get rid of `new[]`, use `std::vector`. Use [constructor lists](https://en.cppreference.com/w/cpp/language/constructor) rather than assignment in your constructor. Use `const` on arguments that aren't mutable, and flag functions that do not alter data as `const`. Use `x[i] `instead of `*(x + i)`. – tadman Jan 16 '21 at 16:34
  • Another thing to keep in mind is when writing non-trivial C++ code you probably want some kind of [unit tests](https://github.com/catchorg/Catch2) to be sure your code works correctly. – tadman Jan 16 '21 at 16:37
  • Did run your code in a **debugger** to see where that error occurs, then run it again with a breakpoint near that failure so you can step carefully ahead and watch what happens leading up to that point? – tadman Jan 16 '21 at 16:38
  • 1
    I have a feeling the problem results from shared pointers and a double free caused by not having a proper copy constructor. Any time you use `new` or `new[]` in your class you will face this problem if you're not extremely careful. – tadman Jan 16 '21 at 16:39
  • Looks like your class violates the rule of 3/5/0: [https://en.cppreference.com/w/cpp/language/rule_of_three](https://en.cppreference.com/w/cpp/language/rule_of_three) – drescherjm Jan 16 '21 at 16:41
  • I actually ran it through the debugger, and the break point occurs at the end of the destructor! – Hardik Phalet Jan 16 '21 at 18:26

0 Answers0