Connecting Tech Pros Worldwide Help | Site Map

code clean up for a static instance of a class

  #1  
Old December 6th, 2006, 04:05 PM
wong_powah@yahoo.ca
Guest
 
Posts: n/a
I need to clean up some old code which implements a static instance of
a class.
Please comment out whether my clean up is good.
My goal is to change the old code as little as possible.
Old code are:
Appliance.h
class Appliance {
//...
private:
static FastMap* ReaderSerialNumberMapping;
};

Appliance.cpp
FastMap* Appliance::ReaderSerialNumberMapping=NULL;


Appliance::Appliance(unsigned int serialNumber) {
if( ReaderSerialNumberMapping==NULL )
{
ReaderSerialNumberMapping = new FastMap(16);
}
//...
ReaderSerialNumberMapping->Assign( serialNumber, readerNumber );
//...
}

void Appliance::doY() {
//...
readerNumber = ReaderSerialNumberMapping->get( serialNumber);
//...
}

// The old code does not have a destructor so it does not delete the
memory pointed by the ReaderSerialNumberMapping. Therefore I want to
fix the memory leak problem by changing the implementation of
ReaderSerialNumberMapping to not using the pointer.
// New code are as follows:
Appliance.h
class Appliance {
//...
private:
static FastMap ReaderSerialNumberMapping;
}

Appliance.cpp
FastMap Appliance::ReaderSerialNumberMapping = FastMap(16);

void Appliance::Appliance(unsigned int serialNumber) {
//...
ReaderSerialNumberMapping.Assign( serialNumber, readerNumber );
}

void Appliance::doY() {
//...
readerNumber = ReaderSerialNumberMapping.get( serialNumber);
//...
}

  #2  
Old December 6th, 2006, 04:45 PM
Ondra Holub
Guest
 
Posts: n/a

re: code clean up for a static instance of a class



wong_powah@yahoo.ca napsal:
Quote:
I need to clean up some old code which implements a static instance of
a class.
Please comment out whether my clean up is good.
My goal is to change the old code as little as possible.
Old code are:
Appliance.h
class Appliance {
//...
private:
static FastMap* ReaderSerialNumberMapping;
};
>
Appliance.cpp
FastMap* Appliance::ReaderSerialNumberMapping=NULL;
>
>
Appliance::Appliance(unsigned int serialNumber) {
if( ReaderSerialNumberMapping==NULL )
{
ReaderSerialNumberMapping = new FastMap(16);
}
//...
ReaderSerialNumberMapping->Assign( serialNumber, readerNumber );
//...
}
>
void Appliance::doY() {
//...
readerNumber = ReaderSerialNumberMapping->get( serialNumber);
//...
}
>
// The old code does not have a destructor so it does not delete the
memory pointed by the ReaderSerialNumberMapping. Therefore I want to
fix the memory leak problem by changing the implementation of
ReaderSerialNumberMapping to not using the pointer.
// New code are as follows:
Appliance.h
class Appliance {
//...
private:
static FastMap ReaderSerialNumberMapping;
}
>
Appliance.cpp
FastMap Appliance::ReaderSerialNumberMapping = FastMap(16);
>
void Appliance::Appliance(unsigned int serialNumber) {
//...
ReaderSerialNumberMapping.Assign( serialNumber, readerNumber );
}
>
void Appliance::doY() {
//...
readerNumber = ReaderSerialNumberMapping.get( serialNumber);
//...
}
I think it's ok. In case you need static variable as pointer, you can
use function atexit to destroy it or wrap it inside the std::auto_ptr.

  #3  
Old December 6th, 2006, 06:47 PM
dasjotre
Guest
 
Posts: n/a

re: code clean up for a static instance of a class



wong_powah@yahoo.ca wrote:
Quote:
I need to clean up some old code which implements a static instance of
a class.
Please comment out whether my clean up is good.
My goal is to change the old code as little as possible.
Old code are:
Appliance.h
class Appliance {
//...
private:
static FastMap* ReaderSerialNumberMapping;
};
>
Appliance.cpp
FastMap* Appliance::ReaderSerialNumberMapping=NULL;
>
>
Appliance::Appliance(unsigned int serialNumber) {
if( ReaderSerialNumberMapping==NULL )
{
ReaderSerialNumberMapping = new FastMap(16);
}
//...
ReaderSerialNumberMapping->Assign( serialNumber, readerNumber );
//...
}
>
void Appliance::doY() {
//...
readerNumber = ReaderSerialNumberMapping->get( serialNumber);
//...
}
>
// The old code does not have a destructor so it does not delete the
memory pointed by the ReaderSerialNumberMapping. Therefore I want to
fix the memory leak problem by changing the implementation of
ReaderSerialNumberMapping to not using the pointer.
// New code are as follows:
Appliance.h
class Appliance {
//...
private:
static FastMap ReaderSerialNumberMapping;
}
>
Appliance.cpp
FastMap Appliance::ReaderSerialNumberMapping = FastMap(16);
>
void Appliance::Appliance(unsigned int serialNumber) {
//...
ReaderSerialNumberMapping.Assign( serialNumber, readerNumber );
}
>
void Appliance::doY() {
//...
readerNumber = ReaderSerialNumberMapping.get( serialNumber);
//...
}
The original implementation had a life-time functionality that your's
doesn't. if you want to limit the keyboard time it would be simplest
to change the ReaderSerialNumberMapping to auto_ptr and carry on from
there. Otherwise it seems to be equivalent.

  #4  
Old December 6th, 2006, 10:06 PM
wong_powah@yahoo.ca
Guest
 
Posts: n/a

re: code clean up for a static instance of a class


dasjotre wrote:
Quote:
The original implementation had a life-time functionality that your's
doesn't.
What is your meaning of "life-time functionality"?
My implementation use the "class static data member" mechanism. It
acts as a global object that belongs to its class type. Its life time
is the same as a global object. However, it is not entered into the
program's global namespace.

  #5  
Old December 6th, 2006, 10:15 PM
Puppet_Sock
Guest
 
Posts: n/a

re: code clean up for a static instance of a class


wong_powah@yahoo.ca wrote:
Quote:
dasjotre wrote:
Quote:
The original implementation had a life-time functionality that your's
doesn't.
>
What is your meaning of "life-time functionality"?
My implementation use the "class static data member" mechanism. It
acts as a global object that belongs to its class type. Its life time
is the same as a global object. However, it is not entered into the
program's global namespace.
The original didn't create the pointed-at FastMap till the first time
the containing class got an instance created. Your implementation
creates it earlier because it is a static. That's not necessarily a
problem, just that you should be aware of the time-of-creation
issue that is going on here. For example, there might be side
effects of creating the FastMap, and these might be important.
Or, it might be that your program could possibly run without
ever actually creating an instance of the containing class, and
so you might not actually ever need the FastMap. Again, all
that might not matter, you should just be aware of it in case it
actually does matter.
Socks

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
" //Clean Up managed resources " f&*ck Koliber (js) answers 25 May 23rd, 2007 03:15 PM
Requesting advice how to clean up C code for validating string represents integer robert maas, see http://tinyurl.com/uh3t answers 232 April 16th, 2007 04:55 AM
Help Needed with Static Object Clean Up. Rick answers 4 November 18th, 2005 02:45 AM
How can I clean up a long-running task before the process ends? Jeffrey Palermo, MCAD.Net answers 2 November 16th, 2005 10:37 PM