How do I free pVect so that memory can be used again?

36 Views Asked by At

The code is

Vect::~Vect()
/*
PRE:  None
POST: free pVect
*/
{
    cout << "(__||  - DELETE (unlock the memory for): " << (*this) << endl;
    // Put code below ....


}

Basically, I need it to correctly free pVect so that my other fuctions can work.

Also there is a .h file with this double* pVect;

1

There are 1 best solutions below

0
Joseph Larson On

It would have been nice if you'd provided a full but minimally-producable example to start with. Let's try this:

class Vect {
public:
    ~ Vect();
private:
    double *pVect = nullptr;
};

Vect::~ Vect() {
    delete [] pVect;
}

Make sure you keep pVect equal to a nullptr unless it contains a valid address. Which means you absolutely should give it a default value like I did.