小型 C 程序生成 500 多个 mallocs?
Small C program makes 500+ mallocs?
我学C有一段时间了,正在为一个项目写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 的。
这不是您的代码;这是泄漏的启动代码。对您来说至关重要的信息是:
==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 有一堆 'in use' 或 'suppressed' 内存是正常的。
当我 运行 你的代码的这个被轻度破解的版本时:
#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)
$
你内置的抑制文件比我的好。
我学C有一段时间了,正在为一个项目写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 的。
这不是您的代码;这是泄漏的启动代码。对您来说至关重要的信息是:
==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 有一堆 'in use' 或 'suppressed' 内存是正常的。
当我 运行 你的代码的这个被轻度破解的版本时:
#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)
$
你内置的抑制文件比我的好。