C 为什么valgrind报告我的记忆为;“肯定输了吗?”;?
考虑以下代码:C 为什么valgrind报告我的记忆为;“肯定输了吗?”;?,c,memory-leaks,valgrind,C,Memory Leaks,Valgrind,考虑以下代码: #include <stdlib.h> int* alloc() { return malloc(250 * sizeof(int)); } int main() { int i; int *vars[3]; for(i = 0; i < 3; ++i) { vars[i] = alloc(); } } 根据: 如果适当设置了泄漏检查,则对于每个剩余的块, Memcheck确定是否可以从内存中的指针访
#include <stdlib.h>
int* alloc()
{
return malloc(250 * sizeof(int));
}
int main()
{
int i;
int *vars[3];
for(i = 0; i < 3; ++i) {
vars[i] = alloc();
}
}
根据:
如果适当设置了泄漏检查,则对于每个剩余的块,
Memcheck确定是否可以从内存中的指针访问该块
根集。根集由(a)个通用寄存器组成
所有线程,以及(b)初始化、对齐、指针大小的数据字
可访问的客户端内存,包括堆栈
据我所知,由于main()
函数的堆栈中仍然指向“肯定丢失”的内存,所以它们应该被归类为“仍然可访问”,对吗
如果没有,我如何配置Valgrind来尝试从main
的堆栈访问内存块,以确定它们是否“仍然可以访问”
编辑:
请不要告诉我释放
main
末尾的指针,这不是我要问的。关于Valgrind术语中“仍然可以到达”和“肯定丢失”之间的区别,请参见以下答案:当main
堆栈被销毁时,即当它返回时,您的内存肯定会丢失。因此,解决方案是不返回
#include <stdlib.h>
int main()
{
/* your code here */
exit(0);
}
当主进程结束时,您肯定失去了释放已分配内存的任何机会,因此这是有意义的。释放方法末尾的三个块,错误就会消失。我的答案不是正确的。我删除了它。当主程序结束时,程序结束,内存将被释放。看见
==5035== Memcheck, a memory error detector
==5035== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==5035== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==5035== Command: ./a.out
==5035==
==5035==
==5035== HEAP SUMMARY:
==5035== in use at exit: 3,000 bytes in 3 blocks
==5035== total heap usage: 3 allocs, 0 frees, 3,000 bytes allocated
==5035==
==5035== LEAK SUMMARY:
==5035== definitely lost: 0 bytes in 0 blocks
==5035== indirectly lost: 0 bytes in 0 blocks
==5035== possibly lost: 0 bytes in 0 blocks
==5035== still reachable: 3,000 bytes in 3 blocks
==5035== suppressed: 0 bytes in 0 blocks
==5035== Reachable blocks (those to which a pointer was found) are not shown.
==5035== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==5035==
==5035== For counts of detected and suppressed errors, rerun with: -v
==5035== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)