2017-02-25 25 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可能会警告您内存泄漏。