链表中的内存泄漏使用动态分配来删除元素

chl*_*314 1 c++ memory-leaks linked-list

我一直在研究链表问题,分别使用指向列表头部和尾部的两个指针.我遇到的问题如下:

当我想从列表的前面或后面删除时,当使用临时动态分配的节点指针实现相应的类方法时,我得到内存泄漏,我无法找出导致泄漏的问题究竟是什么.

#include <iostream>

class Node {
   public:
      Node():data(0),ptrNode(nullptr){};
      ~Node() {};

      // declaring the getters and setters
      float getData() const {return data;};
      void setData(float d) {data = d;};
      Node* getNodePtr() const {return ptrNode;};
      void setNodePtr(Node* n){ptrNode = n;};

   private:
      float data;
      Node* ptrNode;
    };


class List {

   private:
      Node * ptrHead;
      Node * ptrTail;


   public:
      List():ptrHead(nullptr),ptrTail(nullptr) {};
      ~List() {};

      void insertAtFront(float x) {
               Node * temp = new Node();
               temp->setData(x);

               if (ptrHead == nullptr) {
                     ptrHead = temp;
                     ptrTail = temp;
               } else {
                     temp->setNodePtr(ptrHead);
                     ptrHead = temp;
               }
      };

      void insertAtBack(float x) {
               Node * temp = new Node();
               temp->setData(x);

               if (ptrHead == nullptr) {
                     ptrHead = temp;
                     ptrTail = temp;
               } else {
                     ptrTail->setNodePtr(temp);
                     ptrTail = temp;
               }
      };

      void removeFromFront() {

               if (ptrHead==nullptr) {  // if list is empty
                    std::cout << "List is already empty" << std::endl;
                    return;
               }

               if (ptrHead==ptrTail) { // if list has one element
                    delete ptrHead;
                    ptrHead = nullptr;
                    ptrTail = nullptr;
                    return;
               }
               // assign current Head to temp, assign next node to head 
               // delete temp
               Node * temp = new Node();
               temp = ptrHead;
               ptrHead = ptrHead->getNodePtr();
               delete temp;
               temp = nullptr;
      };

      void removeFromBack() {

               if (ptrHead==nullptr) {  // if list is empty
                    std::cout << "List is already empty" << std::endl;
                    return;
               }

               if (ptrHead==ptrTail) { // if list has one element
                    delete ptrHead;
                    ptrHead = nullptr;
                    ptrTail = nullptr;
                    return;
               }

               // create two temp Node pointers for this one
               Node * sec2last = new Node();
               Node * last = new Node();

               sec2last = ptrHead;
               last = ptrTail;

               // locate second to last element and assign it to sec2last
               while (sec2last->getNodePtr() != ptrTail) {
                     sec2last = sec2last->getNodePtr();
               }

               ptrTail = sec2last;
               ptrTail->setNodePtr(nullptr);

               delete last;
               delete sec2last;
               last = nullptr;
               sec2last = nullptr;
      };

   };
Run Code Online (Sandbox Code Playgroud)

我在main()中运行以下命令:

// global function that dynamically allocates memory in its scope
// for a linked list
void generateList(int x) {
   if (x<=0) {
      cout << "Give a positive integer!" << endl;
      return;
   }

   List * aList = new List();

   for (int i = 0;i<x;i++) {
      aList->insertAtFront(i+1);
   }

   aList->removeFromBack();  // this causes leaks

   delete aList;
}

// MAIN
int main() {

   for (int i = 0;i<80000000;i++)    
       generateList(1);   // just repeatedly generate dynamic list 
                          // with one element
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

我应该指出,如果我们不使用removeFromFront()和removeFromBack()方法中的临时节点动态分配内存,那么程序运行正常.但是,就像我说的那样,它让我无法理解为什么我们泄漏了上面的代码.

谢谢!

Dav*_*rtz 5

           Node * temp = new Node();
           temp = ptrHead;
Run Code Online (Sandbox Code Playgroud)

这是内存泄漏.您分配一个新的Node并存储指向它的指针temp.然后覆盖该指针并泄漏刚刚分配的节点.

           // create two temp Node pointers for this one
           Node * sec2last = new Node();
           Node * last = new Node();

           sec2last = ptrHead;
           last = ptrTail;
Run Code Online (Sandbox Code Playgroud)

在这里,你再次这样做.你为什么要在这里分配新Node的?