我正在檢查我的程序是否存在內存泄漏和損壞問題以及 我在使用setpwent時遇到了問題。 考慮簡單的程序爲:setpwent在valgrind中顯示內存泄漏
#include<stdio.h>
#include<stdlib.h>
main()
{
struct passwd *ent=NULL;
setpwent();
while ((ent = getpwent()) != NULL) { }
endpwent();
}
,當我在Valgrind的運行這段代碼,我得到這個:
> valgrind --track-origins=yes
> --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes ./a.out . . . 160 (40 direct, 120 indirect) bytes in 1
> blocks are definitely lost in loss
> record 11 of 11
> ==6471== at 0x4025BD3: malloc (vg_replace_malloc.c:236)
> ==6471== by 0x411CA9C: nss_parse_service_list
> (nsswitch.c:622)
> ==6471== by 0x411D216: __nss_database_lookup (nsswitch.c:164)
> ==6471== by 0x459BEAB: ???
> ==6471== by 0x459C1EC: ???
> ==6471== by 0x411D864: __nss_setent (getnssent_r.c:84)
> ==6471== by 0x40D304F: setpwent (getXXent_r.c:127)
> ==6471== by 0x8048469: main (in /root/workspace/cdk-examples/MMC-0.64/a.out)
> ==6471==
> ==6471== LEAK SUMMARY:
> ==6471== definitely lost: 40 bytes in 1 blocks
> ==6471== indirectly lost: 120 bytes in 10 blocks
> ==6471== possibly lost: 0 bytes in 0 blocks
> ==6471== still reachable: 0 bytes in 0 blocks
> ==6471== suppressed: 0 bytes in 0 blocks
我應該擔心嗎? 我該如何解決這個問題?
第二個問題: 我需要釋放密碼輸入:
main()
{
struct passwd *ent=NULL;
setpwent();
while ((ent = getpwent()) != NULL) {
free(ent);
}
endpwent();
}
謝謝你對我的幫助。
良好和合理的答案,答案 – rahman