gpt4 book ai didi

c++ - valgrind 检测到内存泄漏,但找不到我忘记释放的行

转载 作者:行者123 更新时间:2023-11-30 05:21:33 28 4
gpt4 key购买 nike

<分区>

这里是简化版的代码,我删除了所有与问题无关的内容。

#include <iostream>
#define N 3

int main() {

int *input;
int **cl;

input = new int[N];
cl = new int*[N];
for(int i = 0; i < N; i++) {
cl[i] = new int[N];
}


for(int i = 0; i < N; i++) {
delete[] cl[i];
}
delete[] cl;
delete[] input;

return 0;
}

和 valgrind 输出:

==5782== HEAP SUMMARY:
==5782== in use at exit: 72,704 bytes in 1 blocks
==5782== total heap usage: 6 allocs, 5 frees, 72,776 bytes allocated
==5782==
==5782== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==5782== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5782== by 0x4EC3EFF: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21)
==5782== by 0x40104E9: call_init.part.0 (dl-init.c:72)
==5782== by 0x40105FA: call_init (dl-init.c:30)
==5782== by 0x40105FA: _dl_init (dl-init.c:120)
==5782== by 0x4000CF9: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
==5782==
==5782== LEAK SUMMARY:
==5782== definitely lost: 0 bytes in 0 blocks
==5782== indirectly lost: 0 bytes in 0 blocks
==5782== possibly lost: 0 bytes in 0 blocks
==5782== still reachable: 72,704 bytes in 1 blocks
==5782== suppressed: 0 bytes in 0 blocks
==5782==
==5782== For counts of detected and suppressed errors, rerun with: -v
==5782== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

我刚开始学习C++,可能有一些我看不到的愚蠢错误。

我知道我还应该检查例如分配时是否没有“内存不足”错误,但我忽略它以便代码更清晰。我也知道我可以使用例如vector 并且不关心分配,但我仍然担心我做错了什么。

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