[英]How can I fix the memory leaks of this program, when I know what it leaks, most of the times?
So, I have a dynamic array which leaks memory and I can almost always find out that all memory lost is lost in ONE block, and sometimes it didn't leak anything.所以,我有一个泄漏 memory 的动态数组,我几乎总能发现所有丢失的 memory 都丢失在一个块中,有时它没有泄漏任何东西。 However, I changed something, and now it always leaks, but it's a computable sum (I think it's the size of the array we last get before quitting the program).
但是,我改变了一些东西,现在它总是泄漏,但它是一个可计算的总和(我认为这是我们在退出程序之前最后得到的数组的大小)。 Can you please help me with this?
你能帮我解决这个问题吗?
Here's the code if you want to look into it:如果您想查看代码,请查看以下代码:
#include "DynamicVector.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
DynamicVector createDynamicVector() {
DynamicVector dynamicVector;
dynamicVector.number_of_elements = 0;
dynamicVector.capacity = 2;
dynamicVector.elements = (int *)malloc(dynamicVector.capacity * sizeof(int));
return dynamicVector;
}
int isEmpty(DynamicVector *pointer) {
if (pointer->number_of_elements == 0)
return 1;
return 0;
}
int search_element(DynamicVector *pointer, int element) {
int index;
for (index = 0; index < pointer->number_of_elements; index++) {
if (pointer->elements[index] == element) {
return index;
}
}
return -1;
}
void resize_smaller(DynamicVector *pointer) {
int *new_elements;
new_elements = (int *)malloc((pointer->capacity * 3 / 4) * sizeof(int));
memcpy(new_elements, pointer->elements, pointer->number_of_elements * sizeof(int));
destroyDynamicVector(pointer);
pointer->elements = new_elements;
pointer->capacity = pointer->capacity * 3 / 4;
}
void resize_bigger(DynamicVector *pointer) {
int *new_elements;
new_elements = (int *)malloc(pointer->capacity * 2 * sizeof(int));
memcpy(new_elements, pointer->elements, pointer->number_of_elements * sizeof(int));
destroyDynamicVector(pointer);
pointer->elements = new_elements;
pointer->capacity = pointer->capacity * 2;
}
void add_element(DynamicVector *pointer, int element) {
if (pointer->capacity == pointer->number_of_elements) {
resize_bigger(pointer);
}
*(pointer->elements + pointer->number_of_elements) = element;
pointer->number_of_elements++;
}
int remove_element(DynamicVector *pointer, int element) {
int index_found = 0;
index_found = search_element(pointer, element);
if (index_found == -1) {
return 1;
}
*(pointer->elements + index_found) = *(pointer->elements + pointer->number_of_elements - 1);
pointer->number_of_elements--;
if (pointer->number_of_elements < pointer->capacity / 2) {
resize_smaller(pointer);
}
return 0;
}
void destroyDynamicVector(DynamicVector *pointer) {
free(pointer->elements);
}
The code seems fine, you should post the structure definition and a main()
function that exhibits the leak .代码看起来不错,您应该发布结构定义和
main()
function ,它显示了泄漏。 It is possible that this leak be unrelated to your code: also explain what tool you are using to detect memory leaks.此泄漏可能与您的代码无关:还要说明您使用什么工具来检测 memory 泄漏。
There is a small issue for huge vector sizes where pointer->capacity * 3 / 4
might cause an integer overflow.对于大向量大小存在一个小问题,其中
pointer->capacity * 3 / 4
可能会导致 integer 溢出。 This overflow could cause the malloc()
to fail and return NULL
, prompting undefined behavior when copying the array contents.此溢出可能导致
malloc()
失败并返回NULL
,在复制数组内容时提示未定义的行为。
You could also use realloc()
instead of malloc()
for resizing, saving some copying if the block can be resized in place:您还可以使用
realloc()
而不是malloc()
来调整大小,如果可以就地调整块的大小,则可以节省一些复制:
int resize_bigger(DynamicVector *pointer) {
size_t new_capacity = pointer->capacity * 2;
int *new_elements = realloc(pointer->elements, new_capacity * sizeof(int));
if (new_elements != NULL) {
pointer->elements = new_elements;
pointer->capacity = new_capacity;
return 0;
} else {
return -1;
}
}
void resize_smaller(DynamicVector *pointer) {
size_t new_capacity = pointer->capacity - pointer->capacity / 4;
int *new_elements = realloc(pointer->elements, new_capacity * sizeof(int));
if (new_elements != NULL) {
pointer->elements = new_elements;
pointer->capacity = new_capacity;
}
}
// return the element number or -1 in case of failure
int add_element(DynamicVector *pointer, int element) {
if (pointer->capacity == pointer->number_of_elements) {
if (resize_bigger(pointer))
return -1;
}
pointer->elements[pointer->number_of_elements] = element;
return pointer->number_of_elements++;
}
When you add element, you multiply size by 2 but when you remove element ou keep 3/4.添加元素时,将大小乘以 2,但删除元素时,保留 3/4。 That means that after adding one element and removing element the size will be 2*3/4=1.25 the original size.
这意味着添加一个元素并删除元素后,大小将是原始大小的 2*3/4=1.25。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.