UncleZedz
UncleZedz

Reputation: 21

C++ Read Access Violation,0xCDCDCDCD

I have written a linked list based queue, where each node is linked to the node behind it in the queue. Every other function in the program works with no problems. For some reason this destructor is giving me some issues, and I am not sure why.

I am getting this error:

Exception thrown: read access violation. temp was 0xCDCDCDCD.

Thanks for any help given.

#pragma once

#include <iostream>
#include "Node.h"

template <typename T>
class LQueue
{

    Node<T>* front;
    Node<T>* end;

    int length;

public:

    LQueue();
    ~LQueue();

    //Add item into queue
    void enqueue(T x);

    //Remove item from front of queue
    void dequeue();

    //return item at front of queue
    T peek();

    //Is queue empty?
    bool isEmpty();


    int getLength() { return length; }
};

template<typename T>
inline LQueue<T>::LQueue()
{
    front = nullptr;
    end = nullptr;

    length = 0;
}



template<typename T>
inline void LQueue<T>::enqueue(T x)
{
    Node<T>* temp = new Node<T>;
    temp->data = x;

    length++;

    if (isEmpty())
    {
        temp->next = nullptr;

        front = temp;
        end = temp;
    }
    else
    {
        end->next = temp;
        end = temp;
    }
}

template<typename T>
inline void LQueue<T>::dequeue()
{
    if (isEmpty())
    {
        std::cout << "\n[!] Empty Queue, Nothing To Remove.\n";
        return;
    }

    if (end == front)
    {
        delete front;
        front = nullptr;
        end = nullptr;
    }
    else
    {
        Node<T>* temp = front->next;
        delete front;
        front = temp;
    }
    length--;
}

template<typename T>
inline T LQueue<T>::peek()
{
    return front->data;
}

template<typename T>
inline bool LQueue<T>::isEmpty()
{
    if (front == nullptr)
        return true;
    else
        return false;
}
template<typename T>
inline LQueue<T>::~LQueue()
{
    Node<T>* temp = front;

    while (temp != nullptr)
    {
        Node<T>* temp2 = temp;
        temp = temp->next;

        delete temp2;
    }

}

Error from visual studios

Upvotes: 1

Views: 980

Answers (1)

UncleZedz
UncleZedz

Reputation: 21

Thanks to @Yksisarvinen, I have fixed my problem.

The end->next was only initialized if the queue was empty. I didn't initialize end->next if queue wasn't empty.

Old Code


template<typename T>
inline void LQueue<T>::enqueue(T x)
{
    Node<T>* temp = new Node<T>;
    temp->data = x;

    length++;

    if (isEmpty())
    {
        temp->next = nullptr;

        front = temp;
        end = temp;
    }
    else
    {
        end->next = temp;
        end = temp;
    }
}

New Code:


template<typename T>
inline void LQueue<T>::enqueue(T x)
{
    Node<T>* temp = new Node<T>;
    temp->data = x;

    length++;
    temp->next = nullptr;
    if (isEmpty())
    {


        front = temp;
        end = temp;
    }
    else
    {
        end->next = temp;
        end = temp;
    }
}

Upvotes: 1

Related Questions