Tobias Fizzlewig
Tobias Fizzlewig

Reputation: 111

Unknown cause of Write Access Violation in linked list

In a recent project, the purpose of which is to modify a linked list through a menu function, I'm coming across a single error that I can't seem to fix, as the line of code is purely vital, and, as far as I can tell, should be working as intended. The message I receive is, "Exception Thrown: write access violation. THIS was nullptr." My node class seems to be the source of the error, with the error being presented over the setNext functions body.

#include "pch.h"
#include <iostream>
#include <vector>
#include <string>

template<class ItemType>
class Node
{
private:
    ItemType value;
    Node<ItemType>* next;
public:
    Node() {
        next = nullptr;
    }
    Node(const ItemType& val) {
        value = val;
        next = nullptr;
    }
    Node(const ItemType& val, Node<ItemType>* nextVal)
    {
        value = val;
        next = nextVal;
    }
    void setVal(const ItemType& val)
    {
        value = val;
    }
    ItemType getVal() const {
        return value;
    }
    void setNext(Node<ItemType>* nextVal)
    {
        next = nextVal; //Exception thrown here.
    }
    Node<ItemType>* getNext() const
    {
        return next;
    }
};

Meanwhile, in my linkedlist class, I narrowed down the line that this exception seemed to cause, as the code runs no further beyond it. It thankfully does not involve most of the other classes, but this specific instance seems to react as if the node I call is a null pointer.

template<class ItemType>
class linkChain {
private:
    Node<char>* head;
    int count;
    //Node<ItemType>* getPointerTo(const ItemType& target) const;
public:
    linkChain() {
        head = nullptr;
        count = 0;
    }
    linkChain(std::string phrase) : head(nullptr) {
        count = phrase.length();
        for (int i = count - 1; i >= 0; i--) {
            if (head == nullptr) {
                head = new Node<char>();
                head->setVal(phrase[i]);

            }
            else {
                Node<ItemType>* newNode = new Node<char>(phrase[i]);
                newNode->setNext(head);
                head = newNode;
            }
        }
    }
    void append(const linkChain& chain) {
        count += chain.length();
        Node<char>* newNode = head;
        while (newNode != nullptr) {

            newNode = newNode->getNext();
        }
        Node<char>* nextChain = chain.head;
        while (nextChain != nullptr) {
            char nextValu = nextChain->getVal();
            std::cout << nextValu;
            //May be nextVal
            Node<char>* tempNode = new Node<char>(nextValu);

            //THE LINE BELOW THIS
            newNode->setNext(tempNode);
            //THE LINE ABOVE THIS

            nextChain = nextChain->getNext();
            newNode = newNode->getNext();

        }
        newNode = newNode->getNext();
    }

};

And here is the code used in the main function to call upon this, where charVal is a linkChain.

std::cout << "Please enter a string value to test: ";
            std::cin >> testing;
            linkChain<char> addThis = linkChain<char>(testing);
            std::cout << "Is the string inside: " << charVal->submatch(addThis) << "\n";

I'm not the strongest with nodes, so any advice would be greatly appreciated on how I can fix this error that I am clueless about. Other online answers have given either vague or unrelated answers to this problem, so they have given me little help.

Upvotes: 0

Views: 47

Answers (1)

Adrian Mole
Adrian Mole

Reputation: 51815

The following while loop is one definite problem (without a full MCVE, as requested in the comments, I can't properly check for other errors):

    while (newNode != nullptr) {
        newNode = newNode->getNext();
    }

When this loop has ended, then newNode will be nullptr! But then, in the next loop, a few lines later, you dereference that nullptr value:

while (nextChain != nullptr) {
    //...
    //THE LINE BELOW THIS
    newNode->setNext(tempNode);
    //THE LINE ABOVE THIS
    //...

What you should be doing, in the first while loop, is checking the next pointer:

    while (newNode>getNext() != nullptr) {
        newNode = newNode->getNext();
    }

this loop will exit when you are pointing to the last node in the list.

Upvotes: 3

Related Questions