By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,408 Members | 945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,408 IT Pros & Developers. It's quick & easy.

C++ .. Lint warning

P: n/a
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 ];
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"myName" <my******@myweb.com> wrote in message
news:40***************@myweb.com...
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 ];


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
Jul 22 '05 #2

P: n/a
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/
Jul 22 '05 #3

P: n/a
Ian
myName wrote:
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 ];


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

Jul 22 '05 #4

P: n/a
mea culpa ;)
will have copy c'tor and assignment ops

Tx all
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.