我已经学习了一段时间,我正在为一个项目编写C代码。我主要试图测试结构的用法。我为它写了一个简单的创建和删除方法:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct TokenizerT_ {
int index;
};
typedef struct TokenizerT_ TokenizerT;
TokenizerT *TKCreate( char * ts ) {
TokenizerT *tk = (TokenizerT *)(malloc(sizeof(TokenizerT)));
if(tk == NULL)
return NULL;
tk->index = 0;
return tk;
}
void TKDestroy( TokenizerT * tk ) {
free(tk);
return;
}
int main(int argc, char ** argv)
{
TokenizerT *tk = TKCreate(argv[1]);
printf("%lu\n", sizeof(tk->index));
TKDestroy(tk);
return 0;
}
因此,这会在我的机器上按预期编译和打印4。但是,当我通过valgrind运行时,我得到了这个:
==816== Memcheck, a memory error detector
==816== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==816== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==816== Command: ./test
==816==
--816-- run: /usr/bin/dsymutil "./test"
warning: no debug symbols in executable (-arch x86_64)
4
==816==
==816== HEAP SUMMARY:
==816== in use at exit: 38,504 bytes in 415 blocks
==816== total heap usage: 516 allocs, 101 frees, 45,484 bytes allocated
==816==
==816== LEAK SUMMARY:
==816== definitely lost: 0 bytes in 0 blocks
==816== indirectly lost: 0 bytes in 0 blocks
==816== possibly lost: 0 bytes in 0 blocks
==816== still reachable: 0 bytes in 0 blocks
==816== suppressed: 38,504 bytes in 415 blocks
==816==
==816== For counts of detected and suppressed errors, rerun with: -v
==816== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
我不知道这是否有用,但我的机器是OSX 10.10.5。我真的不知道我的程序如何制作501 mallocs。
答案 0 :(得分:4)
这不是你的代码;它是泄漏的启动代码。关键信息是:
==816== LEAK SUMMARY:
==816== definitely lost: 0 bytes in 0 blocks
==816== indirectly lost: 0 bytes in 0 blocks
==816== possibly lost: 0 bytes in 0 blocks
==816== still reachable: 0 bytes in 0 blocks
==816== suppressed: 38,504 bytes in 415 blocks
特别是最后一行。我使用的Valgrind版本具有相同的版本号但几乎肯定相当老,我不得不添加一堆压制。其他人似乎已经为你做了。
所以,不要担心 - 在Mac OS X上使用一堆&#39;是正常的。或者&#39;抑制&#39;存储器中。
当我运行你的代码的这个温和的黑客版本时:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct TokenizerT_ {
int index;
};
typedef struct TokenizerT_ TokenizerT;
TokenizerT *TKCreate( char * ts );
TokenizerT *TKCreate( char * ts ) {
TokenizerT *tk = (TokenizerT *)(malloc(sizeof(TokenizerT)));
if(tk == NULL)
return NULL;
tk->index = strlen(ts);
return tk;
}
void TKDestroy( TokenizerT * tk );
void TKDestroy( TokenizerT * tk ) {
free(tk);
return;
}
int main(int argc, char ** argv)
{
TokenizerT *tk = TKCreate(argc > 1 ? argv[1] : "CogentConniptions");
printf("%lu\n", sizeof(tk->index));
TKDestroy(tk);
return 0;
}
在我的默认编译选项下,黑客是必要的,以避免编译警告:
$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes \
> -Wold-style-definition -Werror vg.c -o vg
$
当我运行它时,我得到:
$ valgrind vg
==31534== Memcheck, a memory error detector
==31534== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==31534== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==31534== Command: vg
==31534==
--31534-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option
--31534-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 2 times)
--31534-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 4 times)
4
==31534==
==31534== HEAP SUMMARY:
==31534== in use at exit: 38,874 bytes in 418 blocks
==31534== total heap usage: 519 allocs, 101 frees, 45,854 bytes allocated
==31534==
==31534== LEAK SUMMARY:
==31534== definitely lost: 16 bytes in 1 blocks
==31534== indirectly lost: 0 bytes in 0 blocks
==31534== possibly lost: 13,002 bytes in 109 blocks
==31534== still reachable: 25,856 bytes in 308 blocks
==31534== suppressed: 0 bytes in 0 blocks
==31534== Rerun with --leak-check=full to see details of leaked memory
==31534==
==31534== For counts of detected and suppressed errors, rerun with: -v
==31534== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
$
使用我手工制作的suppressions
文件:
$ valgrind --suppressions=suppressions vg
==31538== Memcheck, a memory error detector
==31538== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==31538== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==31538== Command: vg
==31538==
--31538-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option
--31538-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 2 times)
--31538-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 4 times)
4
==31538==
==31538== HEAP SUMMARY:
==31538== in use at exit: 38,874 bytes in 418 blocks
==31538== total heap usage: 519 allocs, 101 frees, 45,854 bytes allocated
==31538==
==31538== LEAK SUMMARY:
==31538== definitely lost: 0 bytes in 0 blocks
==31538== indirectly lost: 0 bytes in 0 blocks
==31538== possibly lost: 0 bytes in 0 blocks
==31538== still reachable: 25,856 bytes in 308 blocks
==31538== suppressed: 13,018 bytes in 110 blocks
==31538== Rerun with --leak-check=full to see details of leaked memory
==31538==
==31538== For counts of detected and suppressed errors, rerun with: -v
==31538== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
$
你内置的抑制文件比我的做得好。