简体   繁体   中英

C++ LinkedList Queue Implementation and Destructor Error:“Aborted (Core Dumped)”

I'm trying to learn about C++ and I am having issues with memory management and the concept of destructors. I have tried to write a Queue implementation using Linked Lists, and I am receiving "Aborted (Core Dumped)" error messages when I try to delete my linked list nodes in my destructor method (bolded). If anyone knows what I'm doing wrong please let me know!

Here's the code for my destructor method:

template <class T>
Queue<T>::~Queue(){
  Node<T> *currHead = head;
  while(currHead != NULL){
    Node<T> *nextHead = currHead->next;
    delete currHead;
    currHead = nextHead;
  }
  Node<T> *currTail = tail;
  while(currTail != NULL){
    Node<T> *nextTail = currTail->next;
    delete currTail;
    currTail = nextTail;
  }
}

And for reference here's my full linkedlist queue implementation:

template <class T>
class Node{
public:
  T data;
  Node<T> *next=NULL;
};

template <class T>
class Queue{
public:
  Queue();
  ~Queue();
  void push(T);
  T pop();
  int size=0;
  Node<T> *head=NULL;
  Node<T> *tail=NULL;
};

template <class T>
Queue<T>::Queue(){}

template <class T>
Queue<T>::~Queue(){
  Node<T> *currHead = head;
  while(currHead != NULL){
    Node<T> *nextHead = currHead->next;
    delete currHead;
    currHead = nextHead;
  }
  Node<T> *currTail = tail;
  while(currTail != NULL){
    Node<T> *nextTail = currTail->next;
    delete currTail;
    currTail = nextTail;
  }
}

template <class T>
void Queue<T>::push(T data){
  Node<T> *node = new Node<T>;
  node->data = data;
  if(head == NULL){
    head = node;
  }else{
    tail->next = node;
  }
  tail = node;
  size++;
}

template <class T>
T Queue<T>::pop(){
  if(size == 0){
    throw "Empty Queue";
  }else{
    Node<T> *oldHead = head;
    T oldData = oldHead->data;
    head = head->next;
    size--;
    delete oldHead;
    return oldData;
  }
}

EDIT:

I've also tried the following definition for the destructor but I am getting the same error:

template <class T>
Queue<T>::~Queue(){

  while(head != NULL){
    Node<T> *currHead = head;
    head = head->next;
    delete currHead;
  }

  while(tail != NULL){
    Node<T> *currTail = tail;
    tail = tail->next;
    delete currTail;
  }
}

In the first part of your destructor you're deleting all the list items starting from head. In the second part of the destructor you're attempting to delete last list item again by using its pointer kept in tail but it's already deleted in the first part.

Deleting a pointer doesn't change the pointer or any other pointers that point to the deleted memory:

#include <iostream>

int main() {
    int* head = new int[4];
    int* tail = head;

    delete head;

    std::cout << "head = " << (void*)head << ", tail = " << (void*)tail << "\n";
}

What your code should be is:

template<typename T>
Queue<T>::~Queue(){
  Node<T> *currHead = head;
  while(currHead != nullptr){
    Node<T> *nextHead = currHead->next;
    delete currHead;
    currHead = nextHead;
  }
  head = tail = nullptr;
  size = 0;
}

or if your pop function works:

template<typename T>
Queue<T>::~Queue() {
    while (size)
        pop();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM