C++ .. Lint warning

M

myName

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 = 0;
}
//...
}

Lint Warning:
=============
Info 1732: new in constructor for class 'StcStackInterface' which has no
assignment operator
sapList = new (StcLowerSapBase*)[ maxStcSaps ];
 
J

John Harrison

myName said:
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 = 0;
}
//...
}

Lint Warning:
=============
Info 1732: new in constructor for class 'StcStackInterface' which has no
assignment operator
sapList = new (StcLowerSapBase*)[ maxStcSaps ];


If a class allocates memory in its constructors and deallocates it in its
destructor, then it generally needs to define a copy constructor and
assignment operator to handle the allocated memory correctly. Without this
the destructor is liable to delete the same memory twice.

At the very least you should declare (but not define) a private copy
constructor and assignment operator, this will prevent you 'accidentally'
copying a StcStackInterface object and crashing your program.

The alternative is not to use raw pointers at all, instead use a smart
pointer like boost::shared_ptr.

john
 
R

Rob Williscroft

myName wrote in 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 = 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.
 
I

Ian

myName said:
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 = 0;
}
//...
}

Lint Warning:
=============
Info 1732: new in constructor for class 'StcStackInterface' which has no
assignment operator
sapList = new (StcLowerSapBase*)[ maxStcSaps ];


You have a class with a pointer member and no copy c'tor or assignment
operator? So lint is warning you that the ownership of sapList is
undefined if you copy a StcStackInterface.

If you don't want them copied, give it a private, unimplemented
assignment operator and copy c'tor.

Ian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top