简体   繁体   English

内存泄漏? 怎么修?

[英]Memory leak? How to fix?

I have my own classes which implements queue and stack using LL, the source code compiles fine on my machine but after throwing it in valgrind it shows me some memory leaks 我有自己的使用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++;
}

瓦尔格隆德

What am i doing wrong? 我究竟做错了什么? Much help would be appreciated :) cheers 许多帮助将不胜感激:)欢呼

In your class constructor: 在您的类构造函数中:

PQ(int cap){
capacity = cap;
arr = new int [capacity++];
for (int i= 0; i < capacity; i++)       arr[i] = {0};}

this: 这个:

capacity++

will first return the capacity and then increase its value by one. 将首先返回容量,然后将其值增加一。 Because of this, when you are filling your array in a for loop, you are going out of array range, because your array size is 1 less than the capacity value. 因此,当您在for循环中填充数组时,由于数组大小比容量值小1 ,因此您超出了数组范围。

This is not a "memory leak". 这不是“内存泄漏”。

This is memory corruption. 这是内存损坏。 You can start fixing it by making a mental effort to understand that arrays in C++ are 0-based, not 1-based. 您可以通过尽一切努力来理解C ++中的数组是基于0的,而不是基于1的数组来开始修复它的。 The first element of an array is array[0] and not array[1] , and everything else is based on that. 数组的第一个元素是array[0]而不是array[1] ,其他所有内容都基于此。 The following is based on the notion that array elements start with array element #1: 以下内容基于数组元素以数组元素#1开头的概念:

int top(){
    return arr[1];
}
void pop(){
    arr[1] = arr[size];

The first element of an array is element #0, not element #1, but this is structured based on the concept that the first element in the array is element #1. 数组的第一个元素是元素#0,而不是元素#1,但这是基于以下概念构造的:数组中的第一个元素是元素#1。

It might seem like adding 1 to the array size before allocating it is an easy way to avoid having to make this adjustment, but it only leads to more grief, confusion, and bugs, later down the line. 似乎在分配数组大小之前先将其加1是避免进行此调整的一种简便方法,但此举只会导致更多的麻烦,困惑和错误,稍后再进行。 This is why, apparently, the constructor attempts to increment the size of the array before allocating it: 显然,这就是为什么构造函数尝试在分配数组之前增加数组大小的原因:

PQ(int cap){
    capacity = cap;
    arr = new int [capacity++];
    for (int i= 0; i < capacity; i++)       arr[i] = {0};
}

Except that it increments it incorrectly. 除了它错误地增加它。 It's a post-increment, so if, for example, cap was 4, new int[4] gets allocated, before capacity gets incremented. 这是后增量,因此,例如,如果cap为4,则会在capacity增加之前分配new int[4] The next line attempts to clear array elements #0 through #4, except that array element #4 doesn't exist, the code tries to initialize it, runs off the end of the array, and valgrind throws a red flag. 下一行尝试清除数组元素#0到#4,除了数组元素#4不存在,代码尝试对其进行初始化,在数组末尾运行,并且valgrind抛出红色标志。

Although this is fixable simply by using pre-increment instead of post-increment, the correct solution is not to increment at all, but restructure the code so that it follows the natural properties of C++ arrays being 0-based, instead of 1-based. 尽管可以简单地通过使用前递增而不是后递增来解决此问题,但是正确的解决方案是根本不递增,而是重新组织代码,以使它遵循C ++数组的自然属性,它们基于0,而不是基于1。 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM