Segmentation fault (core dumped) when I delete pointer

前端 未结 5 1554
北恋
北恋 2021-01-27 04:21

I am trying to remove duplicates from a linked list, and encountered a problem, which is probably obvious and straightforward but I haven\'t used C++ in many years

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-27 04:50

    In this case having the code of the constructor and also of destructor is very important.

    By default you should use "new" in constructor and "delete" in destructor. To be more specific, is very important to allocate all the resources you need in constructor and deallocate in destructor, this way you ensure you don't have any memory leaks.

    free(temp);//You don't need this, also you don't need delete.
    

    Please post your constructor and your destructor code.

    L.E:

    I think you should do something like this:

    template
    class LinkedList
    {
      private:
        struct Node {
            T m_data;
            Node *m_next;
            Node *m_prev;
        };
        Node* m_first;
        Node* m_last;
        int m_count;
      public:
        LinkedList();
        ~LinkedList();
        T GetFirst();
        T GetLast();
        void AddNode(T node);
        void RemoveAt(int index);
        T GetAt(int index);
        int Count();
    };
    
    //constructor
    template 
    LinkedList::LinkedList(){
        m_count = -1;//-1 == "The list is empty!
    }
    
    template 
    void LinkedList::AddNode(T data){
        Node* temp = new Node; //new is called only here -> delete is called in destructor or in RemoveAt
        temp->m_data = data;
        if (m_count > -1){//If the list contains one or more items
            temp->m_next = m_first;
            temp->m_prev = m_last;
            m_last->m_next = temp;
            m_last = temp;
            m_count++;
        }
        else if (m_count == -1)
        {//If no items are present in the list
            m_first = temp;
            m_first->m_next = temp;
            m_first->m_prev = temp;
            m_last = temp;
            m_last->m_next = temp;
            m_last->m_prev = temp;
            m_count = 0;
        }
    }
    
    template 
    void LinkedList::RemoveAt(int index){
        if (index <= m_count)//verify this request is valid
        {
            Node* temp = m_last;
            for (int i = 0; i <= index; ++i){
                temp = temp->m_next;
            }
            temp->m_prev->m_next = temp->m_next;
            temp->m_next->m_prev = temp->m_prev;
            delete temp;
            m_count--;
        }
    }
    
    template 
    T LinkedList::GetAt(int index)
    {
        Node* temp = m_first;
        if (index <= m_count && index > -1){
            int i = 0;
            while(i < index){
                temp = temp->m_next;
                i++;
            }
        }
        return temp->m_data;
    }
    
    template 
    T LinkedList::GetFirst() {
        return m_first->data;
    }
    
    template 
    T LinkedList::GetLast(){
        return m_last->data;
    }
    
    template 
    int LinkedList::Count(){
        return m_count;
    }
    
    template 
    LinkedList::~LinkedList(){
       while(m_count > -1){
            Node* temp = m_first;
            m_first = temp->m_next;
            delete temp;//delete in destructor
            m_count--;
        }
    }
    

提交回复
热议问题