gpt4 book ai didi

c++ - 如何在 GNU C 中使用 mcheck 进行堆一致性检查?

转载 作者:太空宇宙 更新时间:2023-11-04 04:38:47 24 4
gpt4 key购买 nike

我试图了解堆一致性检查在 GNU C 库中的工作原理。我提到了 http://www.gnu.org/software/libc/manual/html_node/Heap-Consistency-Checking.html#Heap-Consistency-Checking

这是我编写的示例程序。如果我在 gdb 中运行并调用 mcheck(0) 我的自定义 abortfn 将被调用,我希望按照手册中的建议。但它没有被调用。

我在这里错过了什么?

包含必要的标题。

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比较方便。

然而,very您链接到的文档说:

it is too late to begin allocation checking once you have allocated anything with malloc

所以开始你的main作为

 int main() {
mcheck(abortfn);

但是,您的abortfn 应该返回void,因此将其编码为:

 void abortfn(enum mcheck_status status) {                                  
switch(status) {
case MCHECK_DISABLED:
printf("MEMCHECK DISABLED\n");
break;
default:
printf("MEMCHECK ENABLED\n");
} }

关于c++ - 如何在 GNU C 中使用 mcheck 进行堆一致性检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28532353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com