现在的位置: 首页 > 综合 > 正文

Linux内存调试工具:valgrind 简单示例

2018年10月04日 ⁄ 综合 ⁄ 共 371字 ⁄ 字号 评论关闭

一、代码

1.1 代码

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char*argv[])
{
	int p = (int)malloc(sizeof(int));
	p = 1234;
	printf("p:%d\n",p);

	return 0;
}

1.2 编译代码

       gcc  -c  test  test.c

二、valgrind使用

2.1 运行代码

      valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./test

2.2 结果

2.3 结果分析

      从上文“total heap usage: 1 allocs, 0 frees, 4 bytes allocated"和"definitely lost: 4 bytes in 1 blocks”可以看出:

      有4个字节的内存泄漏。

抱歉!评论已关闭.