如何使用mcheck的堆一致性检查GNU C? [英] How to use mcheck for Heap Consistency Check in GNU C?
问题描述
我想了解如何在GNU C库中进行堆一致性检查。我参考了 http:// www .gnu.org / software / libc / manual / html_node / Heap-Consistency-Checking.html#Heap-Consistency-Checking
I am trying to understand how heap consistency checking works in GNU C Library. I Refered http://www.gnu.org/software/libc/manual/html_node/Heap-Consistency-Checking.html#Heap-Consistency-Checking
这里是一个示例程序我已经写了。如果我在gdb中运行并调用 mcheck(0)
我的自定义 abortfn
。但它不是被调用。
Here is a sample program I have written. I expected as suggested in the manual if I run in gdb and call mcheck(0)
my custom abortfn
would be called. But it isn't getting called.
我在这里缺少什么?
包含必要的标头。
void *abortfn(enum mcheck_status status)
{
switch(status) {
case MCHECK_DISABLED:
printf("MEMCHECK DISABLED\n");
break;
default:
printf("MEMCHECK ENABLED\n");
}
}
int main()
{
printf("This is mcheck testing code\n");
int *a = (int *) malloc(sizeof(int));
*a = 10;
printf("A: %d\n", *a);
free(a);
return 0;
}
推荐答案
今天, &调试信息( gcc -Wall -Wextra -g
),然后使用 valgrind
Today, compiling with all warnings & debug info (gcc -Wall -Wextra -g
) then using valgrind is more convenient.
但是,非常文档说:
这太晚了开始分配检查一旦你分配了任何与malloc
it is too late to begin allocation checking once you have allocated anything with malloc
那么开始 main
as
int main() {
mcheck(abortfn);
但是, abortfn
c $ c> void 所以将其编码为:
However, your abortfn
should return void
so code it as:
void abortfn(enum mcheck_status status) {
switch(status) {
case MCHECK_DISABLED:
printf("MEMCHECK DISABLED\n");
break;
default:
printf("MEMCHECK ENABLED\n");
} }
这篇关于如何使用mcheck的堆一致性检查GNU C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!