我使用的是 Windows 7 专业版。 我收到了一项任务,需要生成一些系统级问题,以便生成一个我们可以在事件查看器中看到的事件。
我正在测试一种产品(公司产品*无法透露),该产品由一个 dart 组成,可以检查内存泄漏或创建内存泄漏警报。
我编写了一个简单的代码,但我在事件查看器中看不到任何此类事件日志。请向我建议至少应该生成一个事件的任何工具或任何程序。
我的代码:
#include<malloc.h>
#include<stdio.h>
int main() {
for(int i = 0; i < 100; i++) {
int * ptr = (int *) calloc (1000, sizeof(int)); // allocating 40 bytes
// let sizeof int = 4 bytes)
ptr = NULL;
}
return 0;
}
最佳答案
我猜测优化器删除了对 calloc
的调用,因为从它返回的值根本没有被使用。在调用 calloc
之后、ptr = NULL;
之前添加以下行。
printf("%d\n", ptr[0]);
换句话说,检查以下程序:
#include<malloc.h>
#include<stdio.h>
int main() {
for(int i = 0; i < 100; i++) {
int * ptr = (int *) calloc (10, sizeof(int)); // allocating 40 bytes
// let sizeof int = 4 bytes)
printf("%d\n", ptr[0]);
ptr = NULL;
}
return 0;
}
关于c - 执行内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22828378/