我看了好几个问题都有类似的错误,但是我想不通我的问题。我检查了一下,删除后没有访问任何东西,或者尝试删除两次。
类Node和List是我自己实现的,但在这之前的几次练习中我都用过它们,所以我不认为问题出在类的实现上。下面是代码和valgrind给出的错误。顺便说一下,这个程序显然是可行的。
==10365== Invalid read of size 8
==10365== at 0x10992A: Node::getNext() const (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x1099F2: List::clean() (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x10998F: List::~List() (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x10BF5C: main (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== Address 0x5b7e520 is 0 bytes inside a block of size 16 free'd
==10365== at 0x4C3123B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10365== by 0x109A0A: List::clean() (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x10998F: List::~List() (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x10BE24: main (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== Block was alloc'd at
==10365== at 0x4C3017F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10365== by 0x109AA3: List::append(int) (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x10BD8E: main (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365==
==10365== Invalid free() / delete / delete[] / realloc()
==10365== at 0x4C3123B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10365== by 0x109A0A: List::clean() (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x10998F: List::~List() (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x10BF5C: main (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== Address 0x5b7e520 is 0 bytes inside a block of size 16 free'd
==10365== at 0x4C3123B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10365== by 0x109A0A: List::clean() (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x10998F: List::~List() (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x10BE24: main (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== Block was alloc'd at
==10365== at 0x4C3017F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10365== by 0x109AA3: List::append(int) (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x10BD8E: main (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365==
==10365== Invalid read of size 8
==10365== at 0x10992A: Node::getNext() const (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x1099F2: List::clean() (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x10998F: List::~List() (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x10BF6B: main (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== Address 0x5b7e430 is 0 bytes inside a block of size 16 free'd
==10365== at 0x4C3123B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10365== by 0x109A0A: List::clean() (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x10998F: List::~List() (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x10BE15: main (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== Block was alloc'd at
==10365== at 0x4C3017F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10365== by 0x109AA3: List::append(int) (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x10BD52: main (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365==
==10365== Invalid free() / delete / delete[] / realloc()
==10365== at 0x4C3123B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10365== by 0x109A0A: List::clean() (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x10998F: List::~List() (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x10BF6B: main (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== Address 0x5b7e430 is 0 bytes inside a block of size 16 free'd
==10365== at 0x4C3123B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10365== by 0x109A0A: List::clean() (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x10998F: List::~List() (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x10BE15: main (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== Block was alloc'd at
==10365== at 0x4C3017F: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10365== by 0x109AA3: List::append(int) (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365== by 0x10BD52: main (in /mnt/HDD 1TB/TFG/Cracking the code/Chapter 2/prueba)
==10365==
==10365==
==10365== HEAP SUMMARY:
==10365== in use at exit: 0 bytes in 0 blocks
==10365== total heap usage: 31 allocs, 38 frees, 74,736 bytes allocated
==10365==
==10365== All heap blocks were freed -- no leaks are possible
==10365==
==10365== For counts of detected and suppressed errors, rerun with: -v
==10365== ERROR SUMMARY: 14 errors from 4 contexts (suppressed: 0 from 0)
这就是代码。
int getNodeSum(Node * head1, Node * head2, List & sumList) {
int sum = 0;
int digit1 = 0;
int digit2 = 0;
digit1 = head1->getData();
head1 = head1->getNext();
digit2 = head2->getData();
head2 = head2->getNext();
int carry = 0;
if(head1 != 0 && head2 != 0)
carry = getNodeSum(head1, head2, sumList);
sum = digit1 + digit2 + carry;
carry = floor(sum / 10);
sum = sum % 10;
sumList.append(sum);
return carry;
}
List sumNotReverse(List l1, List l2) {
List mySum;
//First we need to pad the lists
int length1 = l1.length();
int length2 = l2.length();
int diff = length1-length2;
if(diff < 0) {
//Then we need to pad list 1
diff = abs(diff);
for(int i = 0; i < diff; i++) {
Node * pad = new Node();
pad->setNext(l1.getHead());
l1.setHead(pad);
}
} else if(diff > 0 ) {
for(int i = 0; i < diff; i++) {
Node * pad = new Node();
pad->setNext(l2.getHead());
l2.setHead(pad);
}
}
int carry = getNodeSum(l1.getHead(), l2.getHead(), mySum);
if(carry == 1)
mySum.append(carry);
//Now we have to reverse the list. There are more/different efficient ways to implement this
stack<int> digits;
Node * aux = mySum.getHead();
Node * prev = aux;
while(aux != 0) {
digits.push(aux->getData());
//Doing this makes space complexity O(N) instead of O(2N)
//(the other way is cleaning the list when we are done)
prev = aux;
aux = aux->getNext();
delete prev;
}
//Make sure head is null
mySum.setHead(0);
while(digits.size() > 0) {
mySum.append(digits.top());
digits.pop();
}
return mySum;
}
我可以解释这段代码的作用, 但有些人觉得这些信息没有用, 所以如果你想要的话, 请告诉我.
根据请求,这里是Node::getNext()、List::clean()和List::~List()的代码。
Node * Node::getNext() const {
return next;
}
List::~List() {
clean();
}
void List::clean() {
if(list != 0) {
Node* aux;
while(list != 0) {
aux = list->getNext();
delete list;
list = aux;
}
}
}
而这是看属性的.h。
class Node {
private:
Node * next;
int data;
public:
Node();
int getData() const;
Node * getNext() const;
void setData(int data);
void setNext(Node * next);
};
class List {
private:
Node * list;
public:
List();
~List();
void clean();
int length() const;
Node * getHead() const;
void setHead(Node * head);
// Insert a node at the end of the list with data given by the argument
void append(int value);
void print() const;
void remove(int pos);
//Remove node toDelete given the reference and the reference to the previous one
//Then, this remove is O(1) time complexity
void remove(Node * prev, Node * toDelete);
void removeDuplicates();
void removeDuplicatesnoBuffer();
};
你可以下载完整的代码来编译它。此处. 在main()中,第136行和164行之间是没有用的,你可以忽略它。这是另一部分练习。
你的 List
类缺乏有效的复制构造函数和赋值操作符,但你却复制了 List
对象,例如当你调用 sumNotReverse
.
这将导致双自由或自由后的使用,这大概就是valgrind抱怨的问题。
更多细节请看这里 什么是The Rule of Three?,或者查阅你喜欢的C++书籍。任何一本像样的C++书都会涉及这个话题。