MILY: 宋體">好了,現在我們可以根據Purify報告修改我們的程序了:
#include <stdio.h> #include <malloc.h> static char *helloWorld = "Hello, World"; main() { char *mystr = malloc(strlen(helloWorld)+1); strncpy(mystr, helloWorld, 12); mystr[12]=”\0”; printf("%s\n", mystr); free(mystr); } |
現在,我們再用Purify重新編譯我們的程序,然后運行,我們可以看到Purify會報告沒有任何的內存問題。其實,使用Purify很簡單,在后面,我將對Purify的各種常用特性做比較全面的闡述。
延伸閱讀
文章來源于領測軟件測試網 http://www.kjueaiud.com/