2017-02-25 163 views
-1

我正在寫一個List類。當我通過Valgrind的運行程序,我得到了以下錯誤:Valgrind:分配一個指針後丟失字節

==26620== 
==26620== HEAP SUMMARY: 
==26620==  in use at exit: 160 bytes in 10 blocks 
==26620== total heap usage: 14 allocs, 4 frees, 82,632 bytes allocated 
==26620== 
==26620== 16 bytes in 1 blocks are definitely lost in loss record 1 of 3 
==26620== at 0x4C2B1EC: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) 
==26620== by 0x10EC98: List<int>::ListNode::addNext(int) (List.h:20) 
==26620== by 0x10E9AB: List<int>::insert(int) (List.cpp:28) 
==26620== by 0x10BA07: main (main.cpp:62) 
==26620== 
==26620== 144 (16 direct, 128 indirect) bytes in 1 blocks are definitely lost in loss record 3 of 3 
==26620== at 0x4C2B1EC: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) 
==26620== by 0x10E93D: List<int>::insert(int) (List.cpp:18) 
==26620== by 0x10BA07: main (main.cpp:62) 
==26620== 
==26620== LEAK SUMMARY: 
==26620== definitely lost: 32 bytes in 2 blocks 
==26620== indirectly lost: 128 bytes in 8 blocks 
==26620==  possibly lost: 0 bytes in 0 blocks 
==26620== still reachable: 0 bytes in 0 blocks 
==26620==   suppressed: 0 bytes in 0 blocks 
==26620== 
==26620== For counts of detected and suppressed errors, rerun with: -v 
==26620== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0) 

按照要求:

List.h

#ifndef CDR_LIST_H 
#define CDR_LIST_H 

template <typename T> 
class List { 
private: 
class ListNode { 
private: 
    T entry; 
    ListNode *next; 
public: 
    ListNode(T entry) : entry(entry), next(nullptr) {} 
    ~ListNode() { 
     if (next != nullptr) { 
      delete next; 
     } 
    } 

    void addNext(T entry) { 
     this->next = new ListNode(entry); // Here 
     setNext(next); 
    } 

    void setNext(ListNode *next) { 
     this->next = next; 
    } 

    ListNode *getNext() { 
     return this->next; 
    } 

    T getEntry() { return this->entry; } 
}; 

ListNode *root; 
int size; 
public: 
List(); 
~List(); 

void deleteList(ListNode *root); 

void insert(T entry); 

T at(int index); 

void deleteAt(int index); 

void printList(); 
}; 
#endif //CDR_LIST_H 

List.cpp

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

template <typename T> 
List<T>::List() : size(0) { 
this->root = nullptr; 
} 

template <typename T> 
List<T>::~List() { 
deleteList(this->root); 
} 

template <typename T> 
void List<T>::insert(T entry) { 
if (this->size == 0 || this->root == nullptr) { 
    root = new ListNode(entry); // And here 
    size++; 
    return; 
} 
ListNode *currNode = this->root; 
while (currNode->getNext() != nullptr) { 
    currNode = currNode->getNext(); 
} 
currNode->addNext(entry); 
this->size++; 
} 

template <typename T> 
T List<T>::at(int index) { 
if (index >= this->size) { 
    throw std::out_of_range("Index out of list range"); 
} 

ListNode *currNode = this->root; 
for (int i = 0; i < index; i++) { 
    currNode = currNode->getNext(); 
} 

return currNode->getEntry(); 
} 

template <typename T> 
void List<T>::deleteList(ListNode *root) { 
if (root == nullptr) { 
    delete root; 
    return; 
} 

deleteList(root->getNext()); 
} 

template <typename T> 
void List<T>::deleteAt(int index) { 
if (index >= this->size) { 
    throw std::runtime_error("Index out of range"); 
} 

ListNode *currNode = this->root; 
ListNode *parentNode = this->root; 
for (int i = 0; i < index; i++) { 
    parentNode = currNode; 
    currNode = currNode->getNext(); 
} 

parentNode->setNext(currNode->getNext()); 
// delete currNode; 
} 

template <typename T> 
void List<T>::printList() { 
ListNode *currNode = this->root; 
while (currNode != nullptr) { 
    std::cout << currNode->getEntry() << std::endl; 
    currNode = currNode->getNext(); 
} 
} 




template class List<int>; 

我明白我創建一個指針就是兩種情況,但由於我需要它指向的數據,所以我不能刪除它們。那麼,這些情況如何被視爲字節丟失,我怎樣才能避免它?

由於

+2

發佈更多代碼。 –

回答

1
void addNext(T entry) { 
     this->next = new ListNode(entry); // Here 
     setNext(next); // this line is unnecessary 
} 

標記行實際上覆蓋這個 - >下一個使用相同的值。雖然它不會改變實際值,但Valgrind可能會警告您內存泄漏。