pclint测试c或c++实例.doc_第1页
pclint测试c或c++实例.doc_第2页
pclint测试c或c++实例.doc_第3页
pclint测试c或c++实例.doc_第4页
pclint测试c或c++实例.doc_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

PC-lint测试C/C+实例实例1:test.cpp1 #include 2 class X3 4 int *p;5 public:6 X()7 p = new int20; 8 void init()9 memset( p, 20, a ); 10 X()11 delete p; 12 ;编译这个文件,VC6.0产生0 errors 0 warnings, 而lint程序产生了如下8条警告信息,有些还是很有用处的提示。PC-lint 告警信息:test.cpp(12): error 783: (Info - Line does not end with new-line)test.cpp(7): error 1732: (Info - new in constructor for class X which has no assignment operator)test.cpp(7): error 1733: (Info - new in constructor for class X which has no copy constructor) memset( p, 20, a ); test.cpp(9): error 669: (Warning - Possible data overrun for function memset(void *, int, unsigned int), argument 3 (size=97) exceeds argument 1 (size=80) Reference: test.cpp: lines 7, 9)test.cpp(7): error 831: (Info - Reference cited in prior message)test.cpp(9): error 831: (Info - Reference cited in prior message) delete p; test.cpp(11): error 424: (Warning - Inappropriate deallocation (delete) for new data) - Wrap-up for Module: test.cpptest.cpp(2): error 753: (Info - local class X (line 2, file test.cpp) not referenced)error 900: (Note - Successful completion,8 messages produced)根据错误提示修改后的程序如下:#include class X /*lint -e753 */ /只声明实现类X,没有写main()应用类x,故可以屏蔽。 int *p;public: X() /构造函数 p = NULL; X (const X &x) /拷贝构造函数 p = new int20; memcpy(p, x.p, 20*sizeof(int); X & operator= (const X &x) /赋值操作符 if (this = &x) /检查自赋值 return *this; int *temp = new int20; memcpy(temp, x.p, 20*sizeof(int); /复制指针指向内容 delete p; /删除原有指针(将删除操作符放在后面,避免X=X特殊情况下,内容的丢失) p=temp; /建立新指向 return *this; void init() if (NULL = p) return; / 判断指针是否为空 memset( p, a, 20*sizeof(int); X() delete p; ;/在;后面回车换行以消除告警test.cpp(12): error 783: (Info - Line does not end with new-line)注意: 为需要动态分配内存的类声明一个拷贝构造函数和一个赋值操作符(可参考effective_c+2e 条款11)再次运行pclintPC-lint for C/C+ (NT) Vers. 9.00a, Copyright Gimpel Software 1985-2008- Module: test.cpp (C+)- Global Wrap-up error 900: (Note - Successful completion, 0 messages produced)实例2:实现输入的两个复数的四则运算。/*File name:Complex.hFunction:A simple complex calculator demo*/#include using namespace std;class complex public: / public interface complex(double r, double i); / constructor void assign(void); / user input to define a complex / overload operators to friend functions friend complex operator + (complex &c1, complex &c2); friend complex operator - (complex &c1, complex &c2); friend complex operator * (complex &c1, complex &c2); friend complex operator / (complex &c1, complex &c2); friend ostream& operator (ostream &out, complex x); private: double real; double imag;/*File name:Complex.cppFunction:A simple complex calculator demo*/#include Complex.hcomplex:complex(double r = 0.0, double i = 0.0) real = r; imag = i; return;void complex:assign(void) cout Please input the real part : real; cout Please input the imaginary part: imag; return;ostream& operator (ostream &out, complex x) if (x.imag 0) return (out x.real + ( x.imag ) i endl); else return (out x.real + x.imag i endl); complex operator + (complex &c1, complex &c2) return complex(c1.real + c2.real, c1.imag + c2.imag); complex operator - (complex &c1, complex &c2) return complex(c1.real - c2.real, c1.imag - c2.imag); complex operator * (complex &c1, complex &c2) return complex(c1.real * c2.real - c1.imag * c2.imag) ,(c1.real * c2. imag + c2.real * c1.imag); complex operator / (complex &c1, complex &c2) double denominator = c2.real * c2.real + c2.imag * c2.imag; return complex(c1.real * c2.real + c1.imag * c2.imag) / denominator ,(c1.imag * c2.real - c1.real * c2.imag) / denominator); int main(void) complex c1(5, 4), c2(2, 10); cout c1 = c1; cout c2 = c2; cout c1 + c2 = c1 + c2; cout c1 - c2 = c1 - c2; cout c1 * c2 = c1 * c2; cout c1 / c2 = c1 / c2; c1.assign(); cout Current c1 = c1; c2.assign(); cout Current c2 = c2; cout c1 + c2 = c1 + c2; cout c1 - c2 = c1 - c2; cout c1 * c2 = c1 * c2; cout c1 / c2 = c1 / c2; return 0;编译这两个文件, VC6.0编译器产生0 errors 0 warnings, 而pclint程序产生了如下27条警告信息, PC-lint 告警信息如下:PC-lint for C/C+ (NT) Vers. 9.00a, Copyright Gimpel Software 1985-2008- Module: Bcomplex.cpp (C+);Bcomplex.h(19): error 783: (Info - Line does not end with new-line)Bcomplex.h(19): error 1712: (Info - default constructor not defined for class complex) Bcomplex.cpp(30): error 1746: (Info - parameter x in function operator(std:basic_ostreamchar,std:char_traits &, complex) could be made const reference)Bcomplex.cpp(35): error 1764: (Info - Reference parameter c1 (line 32) could be declared const ref)Bcomplex.cpp(32): error 830: (Info - Location cited in prior message)Bcomplex.cpp(35): error 1764: (Info - Reference parameter c2 (line 32) could be declared const ref)Bcomplex.cpp(32): error 830: (Info - Location cited in prior message)Bcomplex.cpp(40): error 1764: (Info - Reference parameter c1 (line 37) could be declared const ref)Bcomplex.cpp(37): error 830: (Info - Location cited in prior message)Bcomplex.cpp(40): error 1764: (Info - Reference parameter c2 (line 37) could be declared const ref)Bcomplex.cpp(37): error 830: (Info - Location cited in prior message)Bcomplex.cpp(45): error 1764: (Info - Reference parameter c1 (line 42) could be declared const ref)Bcomplex.cpp(42): error 830: (Info - Location cited in prior message)Bcomplex.cpp(45): error 1764: (Info - Reference parameter c2 (line 42) could be declared const ref)Bcomplex.cpp(42): error 830: (Info - Location cited in prior message)Bcomplex.cpp(51): error 1764: (Info - Reference parameter c1 (line 47) could be declared const ref)Bcomplex.cpp(47): error 830: (Info - Location cited in prior message)Bcomplex.cpp(51): error 1764: (Info - Reference parameter c2 (line 47) could be declared const ref)Bcomplex.cpp(47): error 830: (Info - Location cited in prior message) complex c1(5, 4), c2(2, 10);Bcomplex.cpp(55): error 747: (Info - Significant prototype coercion (arg. no. 1) int to double)Bcomplex.cpp(55): error 747: (Info - Significant prototype coercion (arg. no. 2) int to double)Bcomplex.cpp(55): error 747: (Info - Significant prototype coercion (arg. no. 1) int to double)Bcomplex.cpp(55): error 747: (Info - Significant prototype coercion (arg. no. 2) int to double)- Global Wrap-up error 1754: (Info - Expected symbol operator*= to be declared for class complex) error 1754: (Info - Expected symbol operator+= to be declared for class complex) error 1754: (Info - Expected symbol operator-= to be declared for class complex) error 1754: (Info - Expected symbol operator/= to be declared for class complex) error 900: (Note - Successful completion, 27 messages produced)根据提示修改代码如下:/*File name:Complex.hFunction:A simple complex calculator demo*/#include using namespace std;class complex public: / public interface complex(void); / void constructor complex(double r, double i); / constructor void assign(void); / user input to define a complex / overload operators to friend functions friend complex operator + (const complex &c1, const complex &c2); friend complex operator - (const complex &c1, const complex &c2); friend complex operator * (const complex &c1, const complex &c2); friend complex operator / (const complex &c1, const complex &c2); friend ostream& operator (ostream &out, const complex &x); private: double real; double imag;/*File name:Complex.cppFunction:A simple complex calculator demo*/#include Complex.hcomplex:complex(void) real = 0; imag = 0; return;complex:complex(double r = 0.0, double i = 0.0) real = r; imag = i; return;void complex:assign(void) cout Please input the real part : real; cout Please input the imaginary part: imag; return;ostream& operator (ostream &out, const complex &x) if (x.imag 0) return (out x.real + ( x.imag ) i endl); else return (out x.real + x.imag i endl); complex operator + (const complex &c1, const complex &c2) return complex(c1.real + c2.real, c1.imag + c2.imag); complex operator - (const complex &c1, const complex &c2) return complex(c1.real - c2.real, c1.imag - c2.imag); complex operator * (const complex &c1, const complex &c2) return complex(c1.real * c2.real - c1.imag * c2.imag),(c1.real * c2. imag + c2.real * c1.imag); complex operator / (const complex &c1, const complex &c2) double denominator = c2.real * c2.real + c2.imag * c2.imag; return complex(c1.real * c2.real + c1.imag * c2.imag) / denominator,(c1.imag * c2.real - c1.real * c2.imag) / denominator); int main(void) complex c1(5.0, 4.0), c2(2.0, 10.0); cout c1 = c1; cout c2 = c2; cout c1 + c2 = c1 + c2; cout c1 - c2 = c1 - c2; cout c1 * c2 = c1 * c2; cout c1 / c2 = c1 / c2; c1.assign(); cout Current c1 = c1; c2.assign(); cout Current c2 = c2; cout c1 + c2 = c1 + c2; cout c1 - c2 = c1 - c2; cout c1 * c2 = c1 * c2; cout c1 / c2 = c1 / c2; return 0;再次运行pclint,信息如下:PC-lint for C/C+ (NT) Vers. 9.00a, Copyright Gimpel Software 1985-2008- Module: Complex.cpp (C+)- Global Wrap-up error 1754: (Info - Expected symbol operator*= to be declared for class complex) error 1754: (Info - Expected symbol operator+= to be declared for class complex) error 1754: (Info - Expected symbol operator-= to be declared for class complex) error 1754: (Info - Expected symbol operator/= to be declared for class complex) error 900: (Note - Successful completion, 4 messages produced)编号1754信息:如果类中出现重载操作符时(operator op,op为+、-、*、/),pclint建议声明相应的重载赋值操作符,这里不声明也没问题。可以用/*lint -e1754*/进行屏蔽。参看参看PC-Lint目录下的msg.txt文件,里面有详细的解释。实例3:PC-lint通过检查下面的代码可以发现比其他的C语言编译器发现更多的问题,包括其中的错误和潜在的问题。1:2:char *report( int m, int n, char *p ) 3: 4:int result; 5:char *temp; 6:long nm; 7:int i, k, kk; 8:char name11 = Joe Jakeson; /error 784 向name数组赋值时丢掉了结尾的nul字符9:10:nm = n * m; 11:temp = p = ? null : p; / error 779字符串常量不能用于”=”操作符12:for( i = 0; i 0 ) result = 1; 20:else if( kk 0 ) result = -1; 21:22:if( m = result ) return( temp ); / error 644 result也有可能没有被初始化23:else return( name ); / error 604 返回的是一个局部对象name的地址24: / error 783 没有以新一行结束PC-lint检查信息如下:PC-lint for C/C+ (NT) Vers. 9.00a, Copyright Gimpel Software 1985-2008- Module: test.cpp (C+) char name11 = Joe Jakeson; test.cpp(8): error 784: (Info - Nul character truncated from string) temp = p = ? null : p; test.cpp(10): error 779: (Info - String constant in comparison operator =)test.cpp(5): error 830: (Info - Location cited in prior message) for( i = 0; i 0 ) result = 1; test.cpp(18): error 771: (Info - Symbol kk (line 7) conceivably not initialized)test.cpp(7): error 830: (Info - Location cited in prior message) if( m = result ) test.cpp(21): error 644: (Warning - Variable result (line 4) may not have been initialized)test.cpp(4): error 830: (Info - Location cited in prior message) return( name ); test.cpp(24): error 604: (Warning - Returning address of auto variable name) test.cpp(25): error 783: (Info - Line does not end with new-line)- Global Wrap-up error 900: (Note - Successful completion, 14 messages produced)实例4:#include void find(char arg)switch(arg)case a:printf(an); /没有break,可以用注释/lint fallthrough消除告警825case b:printf(bn); /同上case c: printf(cn); /case控制流一直没被break中断直接进入最后一个case或 /defult,可以用/* fall through */消除告警616 /没有default语句int main(int argc, char* argv) /参数argc和argv没有被引用char buff3 = a, b, c;int i = 0;while(1) /while(1)的问题,请使用for(;)替代find(buffi); printf(n);+i ; /+i使数组越界printf(OK!n); /程序不会被执行到的地方return 0;编译这个文件, VC6.0编译器产生0 errors 0 warnings, 而pclint程序产生了如下14条警告信息, PC-lint 告警信息如下:可见,在这里数组越界pclint同样没有检查出来。PC-lint for C/C+ (NT) Vers. 9.00a, Copyright Gimpel Software 1985-2008- Module: test.cpp (C+) case b:test.cpp(8): error 616: (Warning - control flows into case/default)test.cpp(8): error 825: (Info - control flows into case/default without -fallthrough comment) case c:test.cpp(10): error 616: (Warning - control flows into case/default)test.cpp(10): error 825: (Info - control flows into case/default without -fallthrough comment)test.cpp(12): error 744: (Info - switch statement has no default) while(1)test.cpp(18): error 716: (Info - while(1) . ) printf(OK!n);test.cpp(23): error 527: (Warning - Unreachable code at token printf)test.cpp(25): error 783: (Info - Line does not end with new-line)test.cpp(25): error 715: (Info - Symbol argv (line 14) not referenced)test.cpp(14): error 830: (Info - Location cited in prior message)test.cpp(25): error 818: (Info - Pointer parameter argv (line 14) could be declared as pointing to const)test.cpp(14): error 830: (Info - Location cited in prior message)test.cpp(25): error 715: (Info - Symbol argc (line 14) not referenced)test.cpp(14): error 830: (Info - Location cited in prior message)- Global Wrap-up error 900: (Note - Successful completion, 14 messages produced)根据提示修改代码如下: #include void find(char arg)switch(arg)case a:printf(an);/lint fallthroughcase b:printf(bn);/lint fallthroughcase c:printf(cn);break;default:printf(OKn);int main(int argc, char* argv) (void)argc; / pclint (void)argv; / pclintchar buff3 = a, b, c;int i = 0;for(;)find(buffi);printf(n);if(+i 2) break ;printf(OK!n);return 0;再次运行PClint就没有告警。实例5:#include #include using namespace std;/定义公共基类Personclass Person public: Person(char *nam,char s,int a) /构造函数 strcpy(name,nam);sex=s;age=a; protected: /保护成员 char name20; char sex; int age;/定义类Teacherclass Teacher:virtual public Person /声明Person为公用继承的虚基类 public: Teacher(char *nam,char s,int a,char *t):Person(nam,s,a) /构造函数 strcpy(title,t); protected: /保护成员 char title10; /职称;/定义类Studentclass Student:virtual public Person /声明Person为公用继承的虚基类 public: Student(char *nam,char s,int a,float sco): /构造函数 Person(nam,s,a),score(sco) /初始化表 protected: /保护成员 float score; /成绩 ;/定义多重继承的派生类Graduateclass Graduate:public Teacher,public Student /声明Teacher和Student类为公用继承的直接基类 public: Graduate(char *nam,char s,int a,char *t,float sco,float w): /构造函数 Person(nam,s,a),Teacher(nam,s,a,t),Student(nam,s,a,sco),wage(w) /初始化表 void show( ) /输出研究生的有关数据 coutname:nameendl; coutage:ageendl; coutsex:sexendl; coutscore:scoreendl; couttitle:titleendl; coutwages:wageendl; private: float wage; /工资 ;int main( ) Graduate grad1(Wang-li,f,24,assistant,89.5,1234.5); grad1.show( ); return 0;编译这个文件, VC6.0编译器产生

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论