Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
122 views
in Technique[技术] by (71.8m points)

c++ - Fixing memory leaks in a doubly linked list implementation

I read some of the other posts on this topic because there were quite a few, but they didn't really help my situation.

I am getting memory leaks in my implementation of a doubly linked list. I have to make my own so using list is not an option.

here are the two push functions I am using...

    template <class T>
    void dllist<T>::push_front(T val) {
    node* new_node = new node;
    new_node->value = val;
    new_node->forward = head;
    new_node->backward = nullptr;
    if (head != nullptr)
        head->backward = new_node;

    head = new_node;
    }

and...

    template <class T>
    void dllist<T>::push_back(T val) {
    node* new_node = new node;
    new_node->value = val;
    new_node->forward = nullptr;
    if (!head)
        head = new_node;
    else {
        node* traveller = head;
        while (traveller->forward != nullptr)
            traveller = traveller->forward;
        traveller->forward = new_node;
        new_node->backward = traveller;
       }
    }

finally, here is my destructor

    template <class T>
    dllist<T>::~dllist() {
    node* current = head;
    while (current != nullptr) {
        node* forward = current->forward;
        delete current;
        current = forward;
        }
    }

In main, I declare an object of type dllist called mylist and I make a few calls to push_front with some integer values and then push_back.

I am using the CRT library to check for leaks and there is a leak at each call to push_back or push_front.

I am confused because I thought I made my destructor correctly. Is there something else Im not seeing?

If anyone could point me in the right direction I'd appreciate it!

Thanks.

MRE

template<class T>
class dllist {
    struct node {
        T value;
        node* forward;
        node* backward;
    };
    node* head;
public:
    dllist(); // default constructor
    ~dllist(); // default destructor
    void push_front(T); // push element to the front of the list
    void push_back(T); // push element to the back of the list
};

int main() {
    {
        dllist<int> mylist;
        mylist.push_front(10);
        mylist.push_front(12);
        mylist.push_front(14);
        mylist.push_front(16);
        mylist.push_front(18);
        mylist.push_front(19);
        mylist.push_back(11);
        mylist.push_back(21);
        mylist.push_back(31);
        mylist.push_back(41);
        mylist.push_back(31);
        mylist.push_back(41);
        mylist.push_back(222);
    }
    _CrtDumpMemoryLeaks();
    return 0;
}

template <class T>
dllist<T>::dllist() {
    head = nullptr;
}
question from:https://stackoverflow.com/questions/65862829/fixing-memory-leaks-in-a-doubly-linked-list-implementation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...