我正在写一个 List 类。当我通过 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)
按照要求:
清单.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
列表.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>;
我知道我正在创建一个指针是两种情况,但由于我需要它指向的数据,我无法删除它们。那么,这些情况如何被视为字节丢失,我该如何避免呢?
谢谢
void addNext(T entry) {
this->next = new ListNode(entry); // Here
setNext(next); // this line is unnecessary
}
标记的行实际上用相同的值覆盖了 this->next 。虽然它不会改变实际值,但 Valgrind 可能会警告您内存泄漏。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句