C/C++ÄÚ´æ¹ÜÀíÖ®ÄÚ´æй©¼ì²é
Ò»£® ¸ÅÊö
ÄÚ´æй©һֱÊÇÈí¼þ¿ª·¢ÈËÔ±×îÍ·´óµÄÎÊÌâÖ®Ò»£¬ÓÈÆäÏñC/C++ÕâÑù×ÔÓɶȷdz£´óµÄ±à³ÌÓïÑÔ£¬¼¸ºõÊÇÿһ¸öÓÃÆ俪·¢³öÀ´µÄÈí¼þ¶¼»á³öÏÖÄÚ´æй©µÄÇé¿ö¡£
Èç¹ûûÓÐÄÚ´æй©£¬ÊÀ½ç»òÐí»á±äµÄÃÀºÃ¡£È»¶ø£¬ÍêÈ«ÃÀºÃµÄÊÀ½çÊDz»´æÔڵģ¬ÎÒÃÇÄÜ×öµÄ¾ÍÊǾ¡Á¿ÈÃËü±äµÄ¸üÃÀºÃ¡£
¶þ£® ¹¤¾ß½éÉÜ
×ݹÛÄ¿Ç°ÊÐÃæÉÏÓв»ÉÙÄÚ´æй©¹¤¾ß£¬ÔÚÕâÀï¾Í²»¶à×ö½éÉÜ¡£±ÊÕß×î³£ÓõÄÊÇIBM¹«Ë¾³ö²úµÄIBM Rational Purify£¬ËµÆðPurifyÕⶫÎ÷£¬Îȶ¨ÐÔËäÈ»²»ÔõôÑù£¬µ«¹¦ÄܺÜÇ¿´ó¡£ËüÊÇIBM Rational PurifyPlus²úÆ·×éÖÐÈý½£¿ÍµÄÒ»Ô±¡£
PurifyÊÇÃæÏòC/C++, VB, JavaÓïÑÔ£¬²âÊÔÄÚ´æ´íÎó£¬Windows API·ÃÎÊ´íÎó£¬COM·½·¨µ÷ÓÃÇé¿öµÈ¡£
ͼ1. PurifyÖ÷½çÃæ
Purify¶ÔÓÚC/C++µÄ·ÖÎöÖ§³ÖÔ´´úÂ뼶£¬»òͨ¹ýÆäרÀû¼¼Êõ£º“Ä¿±ê´úÂë²åÈ루Object Code Insertion£©”¹¤×÷ÔÚÎÞÔ´´úÂ뼶¡£PurifyµÄ¹¦ÄÜÆäʵ·Ç³£Ö®Ç¿´ó£¬´Ë´ÎÎÒÃÇÖ»ÌÖÂÛÔõôͨ¹ýPurify½øÐÐC/C++ϵÄÄÚ´æй©·¢ÏÖ¡£ÔÚ½éÉÜËüµÄ¹¤×÷Á÷³ÌÇ°£¬ÎÒÃÇÀ´ÈÏʶһϼ¸¸öËõдÊõÓïµÄÒâ˼£º
l ABR£º Array Bounds Read£¬Êý×éÔ½½ç¶Á
l ABW£ºArray Bounds Write£¬Êý×éÔ½½çд
l FMR£ºFree Memory Read£¬¿Õ¼äÄÚ´æ¶ÁÈ¡
l IPR£º Invalid Pointer Read£¬ÎÞЧָÕë¶ÁÈ¡
l IPW£ºInvalid Pointer Write£¬ÎÞЧָÕëдÈë
l NPR£ºNull Pointer Read£¬¿ÕÖ¸Õë¶ÁÈ¡
l NPW£ºNull Pointer Write£¬¿ÕÖ¸ÕëдÈë
l UMR£ºUninitialized Memory Read£¬Î´³õʼ»¯ÄÚ´æÔĶÁ
l MLK£ºMemory leaks£¬ÄÚ´æй©
l MIU£ºMemory in use, ÄÚ´æʹÓÃÖÐ
l MPK£ºPotential memory leaks£¬Ç±ÔÚµÄÄÚ´æй©
µ±ÎÒÃÇÍê³ÉÁËÄ¿±ê´úÂ룬Éú³É¿ÉÖ´ÐгÌÐòÎļþºó£¬ÔÚPurifyÖмÓÔØËü£¬±ã¿É×Ô¶¯Íê³É¿ÉÖ´ÐгÌÐò½âÎö£¬ÔËÐУ¬·ÖÎöµÄÒ»Ó¦¹¤×÷¡£
ͼ2. Rational PurifyµÄ¹¤×÷Á÷³Ì
Èý£® й©³¡¾°¼°¼ì²â
ÏÖÔÚ£¬ÎÒÃǾÍÀ´×ö¼¸¸ö°¸Àý£¬¿´¿´Ð§¹ûÈçºÎ¡£¿ªÊ¼Ö®Ç°£¬ÏȽéÉÜһϱÊÕߵŤ×÷¼°²âÊÔ»·¾³£º
l Microsfot Windows XP Tablet PC Edition 2005 SP3
l 3GÄÚ´æ(ÔÙ¶àµÄÄÚ´æÒ²ÅÂй©J)
l Microsfot Visual Studio 2005
l IBM Rational Purify 7.0
ÔÚÕâÀïÏë¶îÍâµÄ˵Ã÷һϣ¬±ÊÕßÔø¾²âÊԶԱȹýVC2005ºÍVC6Éú³É³ÌÐò£¬·¢ÏÖVC2005Éú³É»ã±à´úÂëÖÊÁ¿±ÈVC6µÄº
Ïà¹ØÎĵµ£º
C/S¼Ü¹¹µÄÓŵãÈçÏ£º
£¨1£©C/S¼Ü¹¹µÄ½çÃæºÍ²Ù×÷ÊÖÊÆ¿ÉÒÔËæ×Å¿Í»§µÄÒªÇó½øÐж¨ÖÆ£¬¿ÉÒÔʵÏֵķdz£Æ¯ÁÁ£¬ÓÉÓÚÅÖ¿Í»§¶Ë½¨Á¢ÔÚÓ¦ÓõĻù´¡ÉÏ£¬Òò´Ë²»»áÓÉÓÚƯÁÁµÄ½çÃæ¶øÓ°Ïìʵ¼ÊµÄ²Ù×÷¡£
£¨2£©ÏµÍ³°²È«ÐÔÄÜ¿ÉÒԵõ½ºÜºÃµÄ±£Ö¤£¬ÊµÏÖ¶à²ãÈÏÖ¤Ò²²»À§ÄÑ¡£
£¨3£©C/S½á¹¹µÄϵͳÓÉÓÚÖ»ÓÐÒ»²ã½»»¥£¬Òò´ËÏìÓ¦Ëٶȷdz£¿ì¡£
C/S¹¹¼ÜµÄÈ ......
1.ÇóÏÂÃ溯ÊýµÄ·µ»ØÖµ£¨Î¢Èí£©
int func(x)
{
int countx = 0;
while(x)
{
countx
++;
x = x&(x-1);
}
return countx;
}
¼Ù¶¨x = 9999¡£ ´ð°¸£º8
˼·£º½«xת»¯Îª2½øÖÆ£¬¿´º¬ÓеÄ1µÄ¸öÊý¡£
2. ʲôÊÇ“ÒýÓÔ£¿ÉêÃ÷ºÍʹÓÓÒýÓÔҪעÒâÄÄЩÎÊÌ⣿
´ð£ºÒýÓþÍÊÇij¸öÄ¿±ê±äÁ¿µÄ&l ......
//cExample.h
#ifndef C_EXAMPLE_H
#define C_EXAMPLE_H
#ifdef __cplusplus
extern "C"
{
#endif
int add(int x, int y);
#ifdef __cplusplus
}
#endif
#endif
---------------------------------
//cExample.c
#include"cExample.h"
int add(int x, int y)
{
return x + y;
}
----------------- ......
1.ÓÐn¸öÕûÊý£¬Ê¹Ç°Ãæ¸÷Êý˳ÐòÏòºóÒÆm¸öλÖã¬×îºóm¸öÊý±ä³É×îÇ°Ãæm¸öÊý¡£
#include<stdio.h>
int f(int *);
int main()
{
int *p,a[10];
for(p=a;p<a+10;p++)
scanf("%d",p);
f(a);
for(p=a;p<a+10;p++)
printf("%d ",*p);
return 0;
}
int f(int *a)
{
int i,*p ......
ÈçºÎ½»»»Á½¸ö±äÁ¿µÄÖµ£º
CÓïÑÔÖеĴ«Öµ´úÂëÈçÏ£º
int change(int x,int y)
{
int temp=x;
x=y;
y=temp;
}
CÓïÑÔÖеĴ«Ö·´úÂëÈçÏ£º
int change(int *p,int *q)
{
int temp=*p;
*p=*q;
*q=temp;
}
ʹÓÃC++ÖеÄÒýÓÃÀàÐÍ´úÂëÈçÏÂ:
int change(int &x,int &y)
{
int temp=x;
x=y;
y=temp;
}
JAV ......