• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

C/C++内存问题检查利器—Purify (二)

发布: 2009-4-20 17:26 | 作者: 陈皓 | 来源: 测试时代采编 | 查看: 249次 | 进入软件测试论坛讨论

领测软件测试网

三、           MILY: 宋体">示例

假设我们现在有这样一段程序: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程序,就可以看到下面的一个窗口:

 

 

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/

TAG: purify Purify

41/41234>