Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 6th, 2006, 04:05 PM
wong_powah@yahoo.ca
Guest
 
Posts: n/a
Default code clean up for a static instance of a class

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
Default 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
Default 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
Default 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
Default 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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles