gpt4 book ai didi

c - Valgrind 报告的已用内存巨大

转载 作者:行者123 更新时间:2023-12-04 02:40:42 26 4
gpt4 key购买 nike

我正在编写一个版本的 strcat,它可以根据输入字符串的大小分配内存。为此,我使用了 realloc。

我一直在用很多字符串做一些测试。在一些连接 9,193 个字符串的测试中,结果是一个长度为 64,344 的字符串。我用 valgrind 检查了它,但我发现结果有点令人不安......

HEAP SUMMARY
in use at exit: 0 bytes in 0 blocks
total heap usage: 9193 allocs, 9,193 frees, 338,017,768 bytes allocated

All heap blocks were freed -- no leaks are posible

For counts of detected and suppressed errors, rerun with -v
ERROR SUMMARY: 0 Errors form 0 contexts (suppresed: 0 from 0)

在我看来,该程序运行良好且速度可以接受,但我担心分配的 338,017,768 字节消息,我认为这对于简单的字符串连接来说太多了。

“分配的字节数”到底是什么意思??

谢谢。

最佳答案

这是如果您的程序从未调用过free 时本应分配的内存量。它与峰值内存使用无关。

这样做可以得到相同的结果:

for (int i = 0; i < 338017768; ++i) {
free(malloc(1));
}

关于c - Valgrind 报告的已用内存巨大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20007850/

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