473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C++ .. Lint warning

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
4 3814

"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
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
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
mea culpa ;)
will have copy c'tor and assignment ops

Tx all
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Greg Roberts | last post by:
We have a large code base, mainly C but with some C++ which we wish to check for existing issues in an effort to be proactive. I have downloaded the open source GLINT program but am having big...
1
by: Tom | last post by:
I've been using Gimpels PC-Lint static analysis tool (which seems to be extremely good value for money) but I've come up against a couple of problems, although the second is more of a wondering......
2
by: Rahul | last post by:
Hi, I have a little program as follows : =================== STARTS HERE ================ #include <stdio.h> void f (unsigned long); int main() {
4
by: Roman Mashak | last post by:
Hello, All! I often come across the following statements in different source code: #ifndef lint char copyright = "@(#) Copyright (C) 2005 bla-bla-bla\n"; #endif #ifndef lint
1
by: John | last post by:
Does anybody know if there are any compilers that warn of infinite recursion ( a very simple case ) like void foo( int a ) { return foo( a ); }
3
by: copx | last post by:
I saw one here posting the output of a lint tool in a thread, and that made me wonder whether using such tools are still useful. I mean, modern compilers are lint tools themselves, the list of...
4
by: suppamax | last post by:
Hi everybody! I'm using for the first time PC-Lint. I'm analysing my C program for Microchip PIC18F microcontroller. In particualr I have a problem with library functions like "memcpy" or...
15
by: Pat | last post by:
I've been searching for a good multi-module lint checker for Python and I haven't found one yet. Pylint does a decent job at checking for errors only within a single module. Here's one of my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.