【发布时间】:2018-07-27 07:54:34
【问题描述】:
我有自己的类,它使用 LL 实现队列和堆栈,源代码在我的机器上编译得很好,但是在将它扔到 valgrind 之后它显示了一些内存泄漏
class S{
private:
struct Node{
int value;
Node* next;
Node(int v, Node* n):value(v), next(n){}
};
Node* head;
S(const S& other) {}
S& operator=(const S& other) {}
public:
S():head(NULL){}
void push(unsigned int data){
head = new Node(data, head);
}
class Q{
private:
struct Node{
int value;
Node* next;
Node(int v, Node* n):value(v), next(n){}
};
Node* head;
Node* tail;
int size;
Q(const Q& other) {}
Q& operator=(const Q& other) {}
public:
Q():head(NULL), tail(NULL), size(0){}
void push(int data){
if (head == NULL) head = tail = new Node(data, tail);
else{
tail -> next = new Node(data, tail);
tail = new Node(data, tail);
}
size++;
}
我做错了什么?很多帮助将不胜感激:) 干杯
【问题讨论】:
-
消息说“无效写入”,而不是“内存泄漏”。您在构造函数中的数组之外进行编写,因此请花一些时间思考您对
capacity所做的操作以及数组的索引方式。 -
请不要破坏您的帖子。通过在 Stack Exchange 网络上发布,您已授予 SE 分发该内容的不可撤销的权利(在 CC BY-SA 3.0 license 下)。根据 SE 政策,任何破坏行为都将被撤销。
-
为什么要破坏这个?你似乎得到了两个合理的答案。
标签: c++ memory-leaks linked-list valgrind abstract-data-type