C++ Valgrind无效读取大小为8且地址0x5b7e520在大小为16 free'的块内为0字节;D
我读过几个有类似错误的问题,但我无法解决我的问题。我已经检查过,在删除或尝试删除两次后,我没有访问任何内容 类节点和列表是我自己实现的,但是在这之前的几个练习中我都使用过它们,所以我认为问题不在于类的实现。下面是valgrind给出的代码和错误。顺便说一下,这个程序显然有效C++ Valgrind无效读取大小为8且地址0x5b7e520在大小为16 free'的块内为0字节;D,c++,memory-management,valgrind,C++,Memory Management,Valgrind,我读过几个有类似错误的问题,但我无法解决我的问题。我已经检查过,在删除或尝试删除两次后,我没有访问任何内容 类节点和列表是我自己实现的,但是在这之前的几个练习中我都使用过它们,所以我认为问题不在于类的实现。下面是valgrind给出的代码和错误。顺便说一下,这个程序显然有效 ==10365== Invalid read of size 8 ==10365== at 0x10992A: Node::getNext() const (in /mnt/HDD 1TB/TFG/Cracking t
==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;
}
这是查看属性的.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行之间的内容是无用的,您可以忽略它。这是练习的另一部分。您的
列表
类缺少有效的复制构造函数和赋值运算符,但您正在复制列表
对象,例如在调用sumNotReverse
时
这将导致双重免费或免费后使用,这大概是valgrind抱怨的问题
请参阅这里的详细信息,或查阅您最喜欢的C++书籍。任何一本体面的C++书籍都将涉及这个主题。不能帮助你,因为你没有张贴它在栈中的方法:No::GETNEXED(,清单::CURLUTE)和清单::~(ListSee)更一般地说,我们需要一个代码,我们可以编译自己,并运行RunCad,还可以下载和编译代码。在否决投票之前,询问用户,用户将尽快尝试修复它。如果他不这样做,就继续投票,但给他一个机会。我永远不会注意到我的错误就是因为这个。非常感谢你!
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();
};