Joel Gray
Joel Gray

Reputation: 319

How to correctly delete a pointer after using new operator? Code Keeps Throwing Error

So I keep getting the same error when trying to delete pointers after I've used the new operator. I've checked online for ages and can't wrap my head around whats going on, I'm very new to C++ and am working on a university assignment.

Additionally I've successfully used new and delete operators for integer arrays and have not run into issues. So I'm very confused, hopefully I've just done something stupid.

ANY help or direction would be great. For context the program is trying to solve some quadratic equations.

You can see my code and error below. The entire program runs correctly until it get to the destructor and then I get a pop up to say "Exception Thrown: Task2.2.exe has triggered a breakpoint."

The screen then jumps to a break point in delete_scalar.cpp and shows the following:

_CRT_SECURITYCRITICAL_ATTRIBUTE
void __CRTDECL operator delete(void* const block) noexcept
{
    #ifdef _DEBUG
    _free_dbg(block, _UNKNOWN_BLOCK);
    #else
    free(block);
    #endif
}

CODE BELOW:

#include <iostream>
#include <cmath>
using namespace std;

class userValue
{
private:
    double* a;
    double* b;
    double* c;
    double discrim;

public:
    userValue(double*, double*, double*);
    ~userValue();
    void calcDiscrim();
    void displayAns();
};

userValue::userValue(double *x, double *y, double *z)
{
    discrim = 0;
    a = new double;
    a = x;
    cout << "a: " << *a;
    b = new double;
    b = y;
    cout << "b: " << *b;
    c = new double;
    c = z;
    cout << "c: " << *c;
    
}

userValue::~userValue()
{
    delete a, b, c;
}

void userValue::calcDiscrim()
{
    discrim = *b * *b - (4 * *a * *c);
}

void userValue::displayAns()
{
    double numerator, denominator, root1, root2;
    if (discrim > 0)
    {
        cout << "The discriminant: " << discrim << " is nonnegative, now we'll solve the rest of the formula" << endl;
        numerator = (-*b + sqrt(discrim));
        denominator = 2 * *a;
        root1 = numerator / denominator;
        cout << "The first root is: " << root1 << endl;

        numerator = (-*b - sqrt(discrim));
        denominator = 2 * *a;
        root2 = numerator / denominator;
        cout << "The second root is: " << root2 << endl;

    }
    else {
        cout << "The discriminant: " << discrim << " is negative and there are no real roots" << endl;
    }
}

int main()
{
    double a, b, c;
    cout << "Enter the variables to be used in the quadratic equation: " << endl;
    cout << "Enter a value for the variable 'a': ";
    cin >> a;
    cout << "Enter a value for the variable 'b': ";
    cin >> b;
    cout << "Enter a value for the variable 'c': ";
    cin >> c;

    double* j = new double(a);
    double* k = new double(b);
    double* l = new double(c);

    userValue defineInputs(j, k, l);
    defineInputs.calcDiscrim();

    defineInputs.displayAns();

    delete j, k, l;
    return 0;                                               //Return nothing
} 

Upvotes: 0

Views: 398

Answers (1)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275260

The short answer is, remove all pointers, new and delete from your code.

There is no need for it anywhere.

Literally remove every call to both, and remove every * (except when being used to multiply), and your code becomes correct.


Not for this problem, but in other situations, you need to read up on the rule of five, and use unique_ptr by default, and call make_unique instead of new directly.


Specific problems causing your crash include calling new, then immediately assigning over the pointer, losing track of that new pointer. Then you double delete.

But as noted, nothing in your code shown should be using pointers.

Upvotes: 4

Related Questions