示例 假設我們現在有這樣一段程序: hello.c #include s td io.h #include malloc.h static char *helloWorld = "Hello, World"; main() { char *mystr = malloc(strlen(helloWorld)); strncpy(mystr, helloWorld, 12); printf("%s\n", m" name="description" />
假設我們現在有這樣一段程序:hello.c
#include <stdio.h> #include <malloc.h> static char *helloWorld = "Hello, World"; main() { char *mystr = malloc(strlen(helloWorld)); strncpy(mystr, helloWorld, 12); printf("%s\n", mystr); } |
很明顯,這段程序中有內存上的錯誤,假設我們疏忽了這些錯誤,當我們使用Purify進行測試時,我們會發現這段程序中的內存錯誤在Purify下很容易就被找出來了。首先,我們要做的是使用Purify編譯這段程序:
> purify gcc -g -o hello hello.c
Purify 2003.06.00 Solaris 2 (32-bit) Copyright (C) 1992-2002 Rational Software Corp.
All rights reserved.
Instrumenting: cckc6pUD.o Linking
記得加上“-g”的選項,不然,purify不能顯示源程序。好了,現在在當前目錄下產生了可執行文件——hello,在運行hello之前,我們還得注意,執行被Purify編譯過的程序,有可能會出現X-Windows,所以請注意設置DISPLAY環境,如果你是在Windows的Telnet客戶端下執行,那么你可以在Windows下裝一個叫做Exceed的工具軟件,它可以方便地把X-Window顯示在你的Windows桌面上)
好了,我們現在執行hello程序,就可以看到下面的一個窗口:
原文轉自:http://www.anti-gravitydesign.com