myName wrote in news:40***************@myweb.com in comp.lang.c++:
Hi All,
Getting warning in lint for this code. Any thought on this?
Regards
Ajay
StcStackInterface::StcStackInterface()
: genAlarm( false ),
trace( false ),
status( unboundStatus ),
localSuId( 0 ),
evcKernel( 0 )
{
// all the flags are initialized to false
sapList = new (StcLowerSapBase*)[ maxStcSaps ]; // This line giving
warning
for( int i = 0; i < maxStcSaps; i++ ) {
sapList[i] = 0;
}
//...
}
Lint Warning:
=============
Info 1732: new in constructor for class 'StcStackInterface' which has
no assignment operator
sapList = new (StcLowerSapBase*)[ maxStcSaps ];
The problem lint is warning you about simplified:
struct X
{
int *ptr;
X() { ptr = new int(3); }
~X() { delete ptr; }
};
int main()
{
X x, y;
x = y;
/*
Now the int allocated when 'x' was constructed has been lost (leeked)
and at the end of main() ~X() will delete the int allocated when 'y'
was constructed twice since the compiler generated operator = () has
x = y do x.ptr = y.ptr. This *alot* more serious than a leek it a bug
and the programme is broken.
*/
}
To fix 'X' add a user defined copy constructor *and* a user defined
assignment op (X & operator = ( X const & )) to 'X'. If you're never
going to copy or assign X's then make either or both private, in
which case you could just give a declaration with no body.
X fixed:
struct X
{
int *ptr;
X() { ptr = new int(3); }
~X() { delete ptr; }
X( X const & rhs ) : ptr( new int( *rhs.ptr ) ) {}
X &operator = ( X const &rhs )
{
int *p = new int( *rhs.ptr );
delete ptr;
ptr = p;
}
};
As you can see, all the above is a maintanance nightmare, so if at
all possible *don't do it*, use the facilities of the standard
library instead.
Consider replacing your 'sapList' above with an STL container
of /smart_ptr's/ :
#include <vector>
#include "boost/shared_ptr.hpp"
struct StcLowerSapBase
{
int x;
};
std::size_t maxStcSaps = 100;
class StcStackInterface
{
std::vector< boost::shared_ptr< StcLowerSapBase > > sapList;
public:
StcStackInterface( /* whatever */ ) : sapList( maxStcSaps )
{
// you're done.
}
};
int main()
{
StcStackInterface object;
}
If you've not encountered 'shared_ptr' before:
http://www.boost.org/libs/smart_ptr/shared_ptr.htm
HTH.
Rob.
--
http://www.victim-prime.dsl.pipex.com/