使用 malloc 声明变量如何导致丢失位?
How declaring a variable with malloc lead to lost bits?
首先,我有 运行 valgrind 来确保(在默认设置下)错误为零。然后,我决定用类似的东西检查是否有泄漏:--leak-check=full
我的代码看起来像 char* variable=malloc(sizeof(char)*(strlen(in)+1));
并且 valgrind 报告内存为 "definitely lost."
我唯一可以访问的其他代码行(在库回调函数中)是声明 in
的代码行。这是一个 void *
类型的函数参数(虽然在这种情况下我希望我们可以安全地假设该值是空终止的。)
有
#include <stdlib.h>
char * G;
int main()
{
char * l = malloc(10);
G = malloc(20);
}
valgrind下的执行给出:
pi@raspberrypi:/tmp $ valgrind --leak-check=full ./a.out
==11087== Memcheck, a memory error detector
==11087== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==11087== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==11087== Command: ./a.out
==11087==
==11087==
==11087== HEAP SUMMARY:
==11087== in use at exit: 30 bytes in 2 blocks
==11087== total heap usage: 2 allocs, 0 frees, 30 bytes allocated
==11087==
==11087== 10 bytes in 1 blocks are definitely lost in loss record 1 of 2
==11087== at 0x4847568: malloc (vg_replace_malloc.c:299)
==11087== by 0x10453: main (mm.c:7)
==11087==
==11087== LEAK SUMMARY:
==11087== definitely lost: 10 bytes in 1 blocks
==11087== indirectly lost: 0 bytes in 0 blocks
==11087== possibly lost: 0 bytes in 0 blocks
==11087== still reachable: 20 bytes in 1 blocks
==11087== suppressed: 0 bytes in 0 blocks
==11087== Reachable blocks (those to which a pointer was found) are not shown.
==11087== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==11087==
==11087== For counts of detected and suppressed errors, rerun with: -v
==11087== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 3)
malloc(10)
肯定丢失了,因为在执行结束时没有办法访问它(这里是main)
malloc(20)
没有丢失,因为仍然可以通过 G
到达
首先,我有 运行 valgrind 来确保(在默认设置下)错误为零。然后,我决定用类似的东西检查是否有泄漏:--leak-check=full
我的代码看起来像 char* variable=malloc(sizeof(char)*(strlen(in)+1));
并且 valgrind 报告内存为 "definitely lost."
我唯一可以访问的其他代码行(在库回调函数中)是声明 in
的代码行。这是一个 void *
类型的函数参数(虽然在这种情况下我希望我们可以安全地假设该值是空终止的。)
有
#include <stdlib.h>
char * G;
int main()
{
char * l = malloc(10);
G = malloc(20);
}
valgrind下的执行给出:
pi@raspberrypi:/tmp $ valgrind --leak-check=full ./a.out
==11087== Memcheck, a memory error detector
==11087== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==11087== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==11087== Command: ./a.out
==11087==
==11087==
==11087== HEAP SUMMARY:
==11087== in use at exit: 30 bytes in 2 blocks
==11087== total heap usage: 2 allocs, 0 frees, 30 bytes allocated
==11087==
==11087== 10 bytes in 1 blocks are definitely lost in loss record 1 of 2
==11087== at 0x4847568: malloc (vg_replace_malloc.c:299)
==11087== by 0x10453: main (mm.c:7)
==11087==
==11087== LEAK SUMMARY:
==11087== definitely lost: 10 bytes in 1 blocks
==11087== indirectly lost: 0 bytes in 0 blocks
==11087== possibly lost: 0 bytes in 0 blocks
==11087== still reachable: 20 bytes in 1 blocks
==11087== suppressed: 0 bytes in 0 blocks
==11087== Reachable blocks (those to which a pointer was found) are not shown.
==11087== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==11087==
==11087== For counts of detected and suppressed errors, rerun with: -v
==11087== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 6 from 3)
malloc(10)
肯定丢失了,因为在执行结束时没有办法访问它(这里是main)
malloc(20)
没有丢失,因为仍然可以通过 G