pclint测试c或c实例.docx
《pclint测试c或c实例.docx》由会员分享,可在线阅读,更多相关《pclint测试c或c实例.docx(27页珍藏版)》请在冰豆网上搜索。
pclint测试c或c实例
PC-lint测试C/C++实例
实例1:
test.cpp
1#include
2classX
3{
4int*p;
5public:
6X()
7{p=newint[20];}
8voidinit()
9{memset(p,20,'a');}
10~X()
11{deletep;}
12};
编译这个文件,VC6.0产生0errors0warnings,而lint程序产生了如下8条警告信息,有些还是很有用处的提示。
PC-lint告警信息:
test.cpp(12):
error783:
(Info--Linedoesnotendwithnew-line)
test.cpp(7):
error1732:
(Info--newinconstructorforclass'X'whichhasnoassignmentoperator)
test.cpp(7):
error1733:
(Info--newinconstructorforclass'X'whichhasnocopyconstructor)
{memset(p,20,'a');}
test.cpp(9):
error669:
(Warning--Possibledataoverrunforfunction'memset(void*,int,unsignedint)',argument3(size=97)exceedsargument1(size=80)[Reference:
test.cpp:
lines7,9])
test.cpp(7):
error831:
(Info--Referencecitedinpriormessage)
test.cpp(9):
error831:
(Info--Referencecitedinpriormessage)
{deletep;}
test.cpp(11):
error424:
(Warning--Inappropriatedeallocation(delete)for'new[]'data)
---Wrap-upforModule:
test.cpp
test.cpp
(2):
error753:
(Info--localclass'X'(line2,filetest.cpp)notreferenced)
error900:
(Note--Successfulcompletion,8messagesproduced)
根据错误提示修改后的程序如下:
#include
classX/*lint-e753*///只声明实现类X,没有写main()应用类x,故可以屏蔽。
{
int*p;
public:
X()//构造函数
{
p=NULL;
}
X(constX&x)//拷贝构造函数
{
p=newint[20];
memcpy(p,x.p,20*sizeof(int));
}
X&operator=(constX&x)//赋值操作符
{
if(this==&x)//检查自赋值
{
return*this;
}
int*temp=newint[20];
memcpy(temp,x.p,20*sizeof(int));//复制指针指向内容
delete[]p;//删除原有指针(将删除操作符放在后面,避免X=X特殊情况下,内容的丢失)
p=temp;//建立新指向
return*this;
}
voidinit()
{
if(NULL==p)return;//判断指针是否为空
memset(p,'a',20*sizeof(int));
}
~X()
{
delete[]p;
}
};
//在};后面回车换行以消除告警test.cpp(12):
error783:
(Info--Linedoesnotendwithnew-line)
注意:
为需要动态分配内存的类声明一个拷贝构造函数和一个赋值操作符(可参考effective_c++2e条款11)
再次运行pclint
PC-lintforC/C++(NT)Vers.9.00a,CopyrightGimpelSoftware1985-2008
---Module:
test.cpp(C++)
---GlobalWrap-up
error900:
(Note--Successfulcompletion,0messagesproduced)
实例2:
实现输入的两个复数的四则运算。
/****************************************
Filename:
Complex.h
Function:
Asimplecomplexcalculatordemo
****************************************/
#include
usingnamespacestd;
classcomplex
{
public:
//publicinterface
complex(doubler,doublei);//constructor
voidassign(void);//userinputtodefineacomplex
//overloadoperatorstofriendfunctions
friendcomplexoperator+(complex&c1,complex&c2);
friendcomplexoperator-(complex&c1,complex&c2);
friendcomplexoperator*(complex&c1,complex&c2);
friendcomplexoperator/(complex&c1,complex&c2);
friendostream&operator<<(ostream&out,complexx);
private:
doublereal;
doubleimag;
};
/****************************************
Filename:
Complex.cpp
Function:
Asimplecomplexcalculatordemo
****************************************/
#include"Complex.h"
complex:
:
complex(doubler=0.0,doublei=0.0)
{
real=r;
imag=i;
return;
}
voidcomplex:
:
assign(void)
{
cout<<"Pleaseinputtherealpart:
"<cin>>real;
cout<<"Pleaseinputtheimaginarypart:
"<cin>>imag;
return;
}
ostream&operator<<(ostream&out,complexx)
{
if(x.imag<0)
{
return(out<}
else
{
return(out<}
}
complexoperator+(complex&c1,complex&c2)
{
returncomplex(c1.real+c2.real,c1.imag+c2.imag);
}
complexoperator-(complex&c1,complex&c2)
{
returncomplex(c1.real-c2.real,c1.imag-c2.imag);
}
complexoperator*(complex&c1,complex&c2)
{
returncomplex((c1.real*c2.real-c1.imag*c2.imag),(c1.real*c2.imag+c2.real*c1.imag));
}
complexoperator/(complex&c1,complex&c2)
{
doubledenominator=c2.real*c2.real+c2.imag*c2.imag;
returncomplex((c1.real*c2.real+c1.imag*c2.imag)/denominator,(c1.imag*c2.real-c1.real*c2.imag)/denominator);
}
intmain(void)
{
complexc1(5,4),c2(2,10);
cout<<"c1="<cout<<"c2="<cout<<"c1+c2="<cout<<"c1-c2="<cout<<"c1*c2="<cout<<"c1/c2="<c1.assign();
cout<<"Currentc1="<c2.assign();
cout<<"Currentc2="<cout<<"c1+c2="<cout<<"c1-c2="<cout<<"c1*c2="<cout<<"c1/c2="<return0;
}
编译这两个文件,VC6.0编译器产生0errors0warnings,而pclint程序产生了如下27条警告信息,PC-lint告警信息如下:
PC-lintforC/C++(NT)Vers.9.00a,CopyrightGimpelSoftware1985-2008
---Module:
Bcomplex.cpp(C++)
};
Bcomplex.h(19):
error783:
(Info--Linedoesnotendwithnew-line)
Bcomplex.h(19):
error1712:
(Info--defaultconstructornotdefinedforclass'complex')
}
Bcomplex.cpp(30):
error1746:
(Info--parameter'x'infunction'operator<<(std:
:
basic_ostream:
char_traits>&,complex)'couldbemadeconstreference)
}
Bcomplex.cpp(35):
error1764:
(Info--Referenceparameter'c1'(line32)couldbedeclaredconstref)
Bcomplex.cpp(32):
error830:
(Info--Locationcitedinpriormessage)
}
Bcomplex.cpp(35):
error1764:
(Info--Referenceparameter'c2'(line32)couldbedeclaredconstref)
Bcomplex.cpp(32):
error830:
(Info--Locationcitedinpriormessage)
}
Bcomplex.cpp(40):
error1764:
(Info--Referenceparameter'c1'(line37)couldbedeclaredconstref)
Bcomplex.cpp(37):
error830:
(Info--Locationcitedinpriormessage)
}
Bcomplex.cpp(40):
error1764:
(Info--Referenceparameter'c2'(line37)couldbedeclaredconstref)
Bcomplex.cpp(37):
error830:
(Info--Locationcitedinpriormessage)
}
Bcomplex.cpp(45):
error1764:
(Info--Referenceparameter'c1'(line42)couldbedeclaredconstref)
Bcomplex.cpp(42):
error830:
(Info--Locationcitedinpriormessage)
}
Bcomplex.cpp(45):
error1764:
(Info--Referenceparameter'c2'(line42)couldbedeclaredconstref)
Bcomplex.cpp(42):
error830:
(Info--Locationcitedinpriormessage)
}
Bcomplex.cpp(51):
error1764:
(Info--Referenceparameter'c1'(line47)couldbedeclaredconstref)
Bcomplex.cpp(47):
error830:
(Info--Locationcitedinpriormessage)
}
Bcomplex.cpp(51):
error1764:
(Info--Referenceparameter'c2'(line47)couldbedeclaredconstref)
Bcomplex.cpp(47):
error830:
(Info--Locationcitedinpriormessage)
complexc1(5,4),c2(2,10);
Bcomplex.cpp(55):
error747:
(Info--Significantprototypecoercion(arg.no.1)inttodouble)
Bcomplex.cpp(55):
error747:
(Info--Significantprototypecoercion(arg.no.2)inttodouble)
Bcomplex.cpp(55):
error747:
(Info--Significantprototypecoercion(arg.no.1)inttodouble)
Bcomplex.cpp(55):
error747:
(Info--Significantprototypecoercion(arg.no.2)inttodouble)
---GlobalWrap-up
error1754:
(Info--Expectedsymbol'operator*='tobedeclaredforclass'complex')
error1754:
(Info--Expectedsymbol'operator+='tobedeclaredforclass'complex')
error1754:
(Info--Expectedsymbol'operator-='tobedeclaredforclass'complex')
error1754:
(Info--Expectedsymbol'operator/='tobedeclaredforclass'complex')
error900:
(Note--Successfulcompletion,27messagesproduced)
根据提示修改代码如下:
/****************************************
Filename:
Complex.h
Function:
Asimplecomplexcalculatordemo
****************************************/
#include
usingnamespacestd;
classcomplex
{
public:
//publicinterface
complex(void);//voidconstructor
complex(doubler,doublei);//constructor
voidassign(void);//userinputtodefineacomplex
//overloadoperatorstofriendfunctions
friendcomplexoperator+(constcomplex&c1,constcomplex&c2);
friendcomplexoperator-(constcomplex&c1,constcomplex&c2);
friendcomplexoperator*(constcomplex&c1,constcomplex&c2);
friendcomplexoperator/(constcomplex&c1,constcomplex&c2);
friendostream&operator<<(ostream&out,constcomplex&x);
private:
doublereal;
doubleimag;
};
/****************************************
Filename:
Complex.cpp
Function:
Asimplecomplexcalculatordemo
****************************************/
#include"Complex.h"
complex:
:
complex(void)
{
real=0;
imag=0;
return;
}
complex:
:
complex(doubler=0.0,doublei=0.0)
{
real=r;
imag=i;
return;
}
voidcomplex:
:
assign(void)
{
cout<<"Pleaseinputtherealpart:
"<cin>>real;
cout<<"Pleaseinputtheimaginarypart:
"<cin>>imag;
return;
}
ostream&operator<<(ostream&out,constcomplex&x)
{
if(x.imag<0)
{
return(out<}
else
{
return(out<}
}
complexoperator+(constcomplex&c1,constcomplex&c2)
{
returncomplex(c1.real+c2.real,c1.imag+c2.imag);
}
complexoperator-(constcomplex&c1,constcomplex&c2)
{
returncomplex(c1.real-c2.real,c1.imag-c2.imag);
}
complexoperator*(constcomplex&c1,constcomplex&c2)
{
returncomplex((c1.real*c2.real-c1.imag*c2.imag),(c1.real*c2.imag+c2.real*c1.imag));
}
complexoperator/(constcomplex&c1,constcomplex&c2)
{
doubledenominator=c2.real*c2.real+c2.imag*c2.imag;
returncomplex((c1.real*c2.real+c1.imag*c2.imag)/denominator,(c1.imag*c2.real-c1.real*c2.imag)/denominator);
}
intmain(void)
{
complexc1(5.0,4.0),c2(2.0,10.0);
cout<<"c1="<cout<<"c2="<cout<<"c1+c2="<cout<<"c1-c2="<cout<<"c1*c2="<