Home > Net >  How to resolve "Unhandled exception thrown: read access violation." for reverse linked lis
How to resolve "Unhandled exception thrown: read access violation." for reverse linked lis

Time:11-03

I am working on reverse linked list on Windows 10 Pro (64 bit) with Visual Studio Community 2019. I would like to know how to resolve the error I get as below. I get below error in the while loop in the member function reverse() in the class List

(*Please assume the list is already prepared like contiguous integer e.g. 0,1,2,3,4,5) Could you please anyone give me some advice? Thank you in advance.

Unhandled exception thrown: read access violation. current was 0xDDDDDDDD.

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

using std::cout;
using std::endl;

template <class Data>

class Node
{
public:
    Node* next;
    Data data;
};

template <class Data>

class List
{

private:
    Node<Data>* head;
    Node<Data>* tail;
    int count;

public:
    //constructor
    List()
    {
        count = 0;
        head = nullptr;
        tail = nullptr;
    }


    int size()
    {
        return count;
    }


    void msg_empty_list()
    {
        cout << "The list is not modified since it is empty." << endl;
    }


    int push_back(Data data)
    {
        Node<Data>* node = new Node<Data>;
        node->data = data;
        node->next = nullptr;

        if (head == nullptr)
        {
            head = node;
            tail = node;
        }
        else if (head != nullptr)
        {
            tail->next = node;
            tail = tail->next;
        }

        count  ;
        return count;
    }


    int push_front(Data data)
    {
        Node<Data>* node = new Node<Data>;
        node->data = data;
        node->next = nullptr;

        if (head == nullptr)
            head = node;
        else if (head != nullptr)
        {
            node->next = head;
            head = node;
        }

        count  ;
        return count;
    }


    int pop_front(void)
    {
        if (head == nullptr)
            return -1;
        else if (head != nullptr)
        {
            Node<Data>* temp;
            temp = head;
            head = head->next;
            delete temp;

            count--;
            return count;
        }
    }


    int pop_back(void)
    {
        if (head == nullptr)
            return -1;
        else if (head != nullptr)
        {
            Node<Data>* temp = head;
            while (temp->next != tail)
                temp = temp->next;

            delete tail;
            tail = temp;

            count--;
            return count;
        }
    }


    int remove_at(int index)
    {
        if (head == nullptr)
            return -1;
        else if (head != nullptr)
        {
            cout << "Specified index = " << index << endl;
            if (index == 0)
            {
                pop_front();
            }
            else if (index == -1)
            {
                pop_back();
            }
            Node<Data>* temp = head;
            Node<Data>* rmv;
            int countIndex = 0;

            while (countIndex < index - 1)
            {
                temp = temp->next;
                countIndex  ;
            }

            rmv = temp->next;
            temp->next = temp->next->next;
            delete rmv;

            count--;
            return count;
        }
    }


    void reverse()
    {
        Node<Data>* temp = nullptr;
        Node<Data>* prev = nullptr;
        Node<Data>* current = head;

        while (current != nullptr)
        {
            temp = current->next; // where I get error "Unhandled exception thrown: read access violation. **current** was 0xDDDDDDDD."
            current->next = prev;
            prev = current;
            current = temp;
        }
        head = prev;
    }


    void print()
    {
        Node<Data>* temp = head;

        if (head == nullptr)
            return;

        for (int i = 0; i < count - 1; i  )
        {
            cout << temp->data << ", ";
            temp = temp->next;
        }

        cout << temp->data;
    }


    ~List()
    {
        Node<Data>* temp = head;

        while (temp->next != nullptr)
        {
            temp = temp->next;
            delete head;
            head = temp;
        }

    }
};


int main()
{
    Node<int> x;
    Node<bool> y;
    Node<char> n;
    List<int> list;

    //insert items into list
    for (int i = 0; i < 10; i  )
    {
        list.push_back(i);
    }

    cout << "Original list[size=" << list.size() << "]: ";
    //print the list
    list.print();
    cout << endl;

    // push a node to the beginning of the list
    cout << endl << "==> Push a node to the head" << endl;
    list.push_front(-1);
    cout << "Modified list[size=" << list.size() << "]: ";
    list.print();
    cout << endl;


    // pop the head of the list
    cout << endl << "==> Pop a node from the head" << endl;
    if (list.size() == 0)
        list.msg_empty_list();
    else
    {
        list.pop_front();
        cout << "Modified list[size=" << list.size() << "]: ";
        list.print();
        cout << endl;
    }

    /*
    // pop the tail of the list
    cout << endl << "==> Pop a node from the tail" << endl;
    if (list.size() == 0)
        list.msg_empty_list();
    else
    {
        list.pop_back();
        cout << "Modified list[size=" << list.size() << "]: ";
        list.print();
        cout << endl;
    }
    */

    // delete the node at the specified index
    cout << endl << "==> Delete the node at the specified index" << endl;
    if (list.size() == 0)
        list.msg_empty_list();
    else
    {
        list.remove_at(5);
        cout << "Modified list[size=" << list.size() << "]: ";
        list.print();
        cout << endl;
    }


    // reverse the list
    cout << endl << "==> Reverse the list" << endl;
    if (list.size() == 0)
        list.msg_empty_list();
    else
    {
        list.reverse();
        cout << "Modified list[size=" << list.size() << "]: ";
        list.print();
        cout << endl;
    }

    return 0;
}

CodePudding user response:

I can spot a few mistakes in your code, but let's concentrate on the pop_back() function:

    int pop_back(void)
    {
        if (head == nullptr)
            return -1;
        else if (head != nullptr) // 1)
        {
            Node<Data>* temp = head;
            while (temp->next != tail) // 2)
                temp = temp->next;

            delete tail;
            tail = temp; // 3)

            count--;
            return count;
        }
    }
  1. Please don't do this: if (a == nullptr) {...} else if (a != nullptr) {...} is redundant. Just leave the second if away. It lets a reader believe there could be a third case.
  2. It may be luck that this usually works, but some of the other methods don't properly update the tail pointer, so this might never be true. Validate your pop_front method when there's only one element in the list. Other functions might have similar problems.
  3. Here's the actual problem you're observing. You're not setting the next pointer of the new tail element to null, instead it points to the now deleted tail. Insert tail->next = nullptr after this line.
  • Related