polynomial polynomial :: derivative(void)
{
polynomial outpoly;
outpoly._coef = new double [_degree];
outpoly._degree = (_degree-1);
for(int i=0; i<(_degree); i++)
{
outpoly._coef[i]=(i+1)*_coef[i+1];
}
return outpoly;
}

#pragma once

#include <complex>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class polynomial
{
private:
double * _coef;
int _degree;
public:
// constructors
polynomial(void);
polynomial(int degree, double * coef);
~polynomial(void);

// at: evaluates the polynomial at x or z
//   returns the real/complex value of the polynomial at x/z
double at(double x) const;
complex<double> at ( complex<double> z) const;

polynomial derivative(void) const;
bool isEmpty(void);
string tostring(void);

polynomial& operator = (const polynomial& rhs);
friend ostream& operator << (ostream& os, polynomial& m);

};

//How I use it in main:
double * coef;
int degree;
cout<< "INPUT degree: ";
cin>>degree;
coef = new double [degree+1];
for(int n = 0; n<(degree+1); n++)
{
cout<< "INPUT " << n << "th degree: ";
cin>> coef[n];
cout<<endl;
}
polynomial poly(degree, coef);
cout<<"INPUT converted to: " << poly<<endl;
cout<<"EVALUATE for x = ";
double x;
cin >> x;
cout << poly << " = " << poly.at(x) << endl;
cout << "DERIVATIVE of fxn is: " << poly.derivative() <<endl;

I have code for handling polynomials via an array of coefficients. Now I want to create a function that returns its derivative in polynomial form.

Unfortunately, the return value is deleted before it is passed on. I discovered this the hardway by following the debugger as closely as I could.

How can I avoid this error?

## All 9 Replies

Uh.... what?

The "outpoly" within the function "derivative" is supposed to return the derivative of the polynomial it is called from.

I followed my debugger and it showed me that the program cleared the memory supposed to be occupied by the data stored in "outpoly." What is then returned is an empty set of data. If I otherwise return the addresses of the data set then, the function returns empty adresses.

Basically, I don't get the desired output I want, even though the result "outpoly" checks ok during runtime of the function. I need to avoid this untimely deletion of data.

I'm sorry if my code looks messy, but I just snipped it from my files. In truth, it would be longer if I put the entire code in. But if you need it to try and resolve my problem then, I will gladly upload it.

At which point does the data disappear? If you run the code to right before the return statement, is the data present?

Are you sure it's not returning a polynomial object OK but the output is getting hosed up with the << operator? Everything should still be in scope at the end of your function.

I checked the debugger and it deletes the data just right before it returns. I don't seem to get errors at cout, but since the data is invalid error does pop up.
I'll check on it more closely, but this is what I've observed so far.

which values are you inspecting with your debugger? Just outpoly, or the members like outpoly._coef?

Your polynomial class has a pointer member and a destructor, but no user-defined copy constructor. Sounds like a one-way ticket to the exact error you're describing.

Checking the variable "outpoly I observe its member variables. Checking with other inputs I observe only the pointer "_coef" becomes erroneous.

Thank you for the suggestion of using an explicit copy constructor. I'll look into it but at the moment I'm in school and our terminals here are more thin clients than functional machines. I can't compile now.

polynomial polynomial :: derivative(void)
{
if(!isEmpty())
{
double *coef;
coef = new double[(_degree-1)];
for(int i=0; i<(_degree); i++)
{
coef[i]=(i+1)*(_coef[i+1]);
}
polynomial outpoly((_degree-1), coef);
return outpoly;
}
else
{
cerr<<"Empty polynomial; unable to get derivative.";
exit(1);
}
}

I revised the code to include exceptions of "empty" polynomials calling its derivative. Unfortunately, the error still happens. I believe the problem lies in the fact that when the instance of the function ends so does any local variables within it. I did some experiment and emptied my destructor ~polynomial, and it returned a valid derivative of the called polynomial.

//experimented as such:
polynomial :: ~polynomial(void)
{
//commented out my destructor to see what happens if destructor is empty.
//delete [] _coef;
//_degree = 0;
}

Is there any other way than emptying my destructor to avoid that untimely deletion?

>I believe the problem lies in the fact that when the instance
>of the function ends so does any local variables within it.

Are you a complete idiot? I told you exactly what the problem was: you don't have a suitable copy constructor. The default copy constructor does not make a copy of dynamic memory, only pointers to said memory. When you return a copy of the polynomial object, the copy points to the original memory, which is deleted when the original object is destroyed. So why don't you fix that before digging any deeper into the mystery, Sherlock.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.