valgrind-3.8.1的安裝與使用研究

目前只知道這個工具可以測試linux下的內存泄漏問題,很好用,下載地址是http://www.valgrind.org/downloads/valgrind-3.8.1.tar.bz2

安裝:


[root@localhost ~]# cd valgrind-3.8.1
[root@localhost valgrind-3.8.1]# ./configure && make && makeinstall  


完成後 

進行測試看是否安裝成功。

[ubuntu@root ~]#valgrind


看結果是否爲:


[ubuntu@root ~]#valgrind
valgrind: no program specified
valgrind: Use --help for more information.


然後就可以對程序就行了測試了:


測試:


3、示例(測試一個C)

#include <stdio.h>                                                                  

#include <stdlib.h>

int main()

{

       struct mm{

                int a;

                int b;

       }kk,*p;

       kk.a = 10;

       printf("kk.a : %d \n",kk.a);

       p = (struct mm *)malloc(sizeof(struct mm));

       p->a = 9;

       printf("p->a : %d \n",p->a);

 

return 0;

}

操作如下:

[root@localhost ~]# vim hello.c

[root@localhost ~]# gcc -g -o hello hello.c

[root@localhost ~]# ./hello


結果如下:

然後用valgrind命令:

[root@localhost ~]# valgrind --tool=memcheck--leak-check=yes --show-reachable=yes ./hello

可以看出上面提示“malloc/free:1 allocs, 0 frees, 8 bytes allocated.”,“definitely

lost:8 bytes in 1 blocks.”。即丟失了8個字節。


例子引用了其他人的。。。。。

這個工具不錯,對於開發arm上這種比較需要注意資源使用的程序,很有幫助

發佈了27 篇原創文章 · 獲贊 1 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章