gpt4 book ai didi

memory-leaks - Valgrind 中丢失了零字节

转载 作者:行者123 更新时间:2023-12-04 10:21:13 24 4
gpt4 key购买 nike

当 Valgrind 报告丢失 o 个字节时,这意味着什么,如下所示:

==27752== 0 bytes in 1 blocks are definitely lost in loss record 2 of 1,532

我怀疑它只是创意使用 malloc 的产物。 , 但最好确定 (-;

编辑:当然,真正的问题是它是否可以被忽略,或者它是一个有效的泄漏,应该通过释放这些缓冲区来修复。

最佳答案

是的,这是真正的泄漏,应该修复。

当您malloc(0) , malloc 可以是 give you NULL, or an address that is guaranteed to be different from that of any other object .

由于您可能使用的是 Linux,因此您将获得第二个。分配的缓冲区本身没有浪费空间,但 libc 必须做一些内务处理,这确实浪费了空间,所以你不能继续做 malloc(0)无限期地。

您可以通过以下方式观察它:

#include <stdio.h>
#include <stdlib.h>
int main() {
unsigned long i;
for (i = 0; i < (size_t)-1; ++i) {
void *p = malloc(0);
if (p == NULL) {
fprintf(stderr, "Ran out of memory on %ld iteration\n", i);
break;
}
}
return 0;
}

gcc t.c && bash -c 'ulimit -v 10240 && ./a.out'
Ran out of memory on 202751 iteration

关于memory-leaks - Valgrind 中丢失了零字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5498395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com