Home > database >  Exception thrown: read access violation. this->top was nullptr
Exception thrown: read access violation. this->top was nullptr

Time:08-18

I am currently trying to develop a RPM Calculator using C but I am running into this error message whenever I try to run my code

Exception thrown: read access violation. this->top was nullptr.

Here is what I have in my .h file:

template<class T>
struct MyStack
{
    T data; // this is going to store the number
    MyStack<T>* link; // used to point to the next element in the stack
    
private:
    MyStack<T>* top = NULL; // this will represent the top of the stack when the stack is empty this is set to null
public:
    void push(T operand);
    void value();
    void pop();
};

template<class T>
void MyStack<T>::push(T value) 
{
    MyStack<T>* ptr = new MyStack<T>; // create a new pointer
    ptr->data = value;  // set data to whatever the value that is passed in
    ptr->link = top; 
    top = ptr;
}


template<class T>
void MyStack<T>::value()
{
    if (top->data != NULL) { // here is where the error occurs
        cout << top->data << ">" << endl;
    }
    else {
        cout << "X>";
    }// prints out the current top value
}


template<class T>
void MyStack<T>::pop()
{
    MyStack<T>* ptr = top; // creating the pointer
    top = top->link; //. top is now equal to the link
    delete(ptr); // I can now delete the top value as top is now equal to the link which is the previos value
}

And here is what I have in my main

int main() 
{
    
    MyStack<double> s;
    string input;

    while (true) {
        // display prompt
        s.value();
    //    cout << "VALUE>";

        // get input
        cin >> input;

        // check for numeric value
        double num;
        if (istringstream(input) >> num) {
            s.push(num);
        }
        // check for operator
        else if (isOperator(input)) {
            performOp(input, s);
        }

        // check for quit
        else if (input == "q") {
            return 0;
        }
        else {
            cout << "Invalid Input" << endl;
        }
    }
    

    
}

Could anyone please help me with this? I have been trying to fix this all day but haven't been able to do it. NOTE: I have removed some other functions I had so the code was not too long any help will be very much appreciated

CodePudding user response:

Your while loop is calling s.value() on its 1st iteration when s is empty, so the s.top field is still NULL, but value() (and also pop()) tries to access top->data (and top->link) regardless of whether top is NULL or not.

You need to fix your MyStack methods to work correctly on an empty stack, ie:

In value():

if (top->data != NULL)

should be

if (top != NULL)

and in pop():

top = top->link;

should be

if (top != NULL) top = top->link;

Otherwise, simply don't call value() (or pop()) when the stack is empty. Add an isEmpty() method that returns true when top is NULL, and then call value() (or pop()) only when isEmpty() returns false:

if (!s.isEmpty())
    s.value();
  • Related