473,395 Members | 1,688 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,395 software developers and data experts.

"Static" destructor

Hi,

I have a class A with a static pointer to some data. The pointer is static
because I want to access the data from all instances of the class. The data
itself is allocated when the first instance of A is constructed:

# a.h

class A
{
private:
static char* sm_data;

public:
A();
};

# a.cpp

A::A()
{
if (sm_data == NULL)
sm_data = new char[1024];
}

char* A::sm_data = NULL;

This works fine, but what should I do to make sure that sm_data is
deallocated when I close the application? I could always use reference
counting and delete [] sm_data when the last instance of A is deleted, but
that is a little cumbersome. I could also have a static function that is
called from the application's main class when it exits, but then I'd have to
rely on other parts of the application.

My question is: how would you solve this?
Regards,
Jahn Otto

Jul 19 '05 #1
6 11704
Oops - typo. I wrote "I could always use reference counting[....]", but I
meant instance counting.
Jahn Otto
Jul 19 '05 #2
Jahn Otto Næsgaard Andersen wrote:
Hi,

I have a class A with a static pointer to some data. The pointer is static
because I want to access the data from all instances of the class. The data
itself is allocated when the first instance of A is constructed:

# a.h

class A
{
private:
static char* sm_data;

public:
A();
};

# a.cpp

A::A()
{
if (sm_data == NULL)
sm_data = new char[1024]; ^^^^^^^ no need for this
}

char* A::sm_data = NULL;
you could do this:

char* A::sm_data = new char[1024];

This works fine, but what should I do to make sure that sm_data is
deallocated when I close the application?
You may not need to if this is a stan-alone application.

I could always use reference counting and delete [] sm_data when the last instance of A is deleted, but
that is a little cumbersome.
Use a helper class - in this case a std::string is great (and reccomended).

class A
{
private:
std::string sm_data;

public:
A();
};

# a.cpp

A::A()
{
}
ok - so if you don't like that because it breaks too much code here is a
bad bad bad hack. (not reccomended)

class A
{
private:
static char* sm_data;

public:
A();

private:
class Nuker
{
public:
~Nuker()
{
delete[] sm_data;
}

Nuker() {};

private:
Nuker( const Nuker & );
Nuker & operator = ( const Nuker & );

};

static Nuker a_nuker;
};

A::A()
{
Nuker & nuker = a_nuker;
}

char* A::sm_data = new char[1024];

A::Nuker A::a_nuker;
I could also have a static function that is called from the application's main class when it exits, but then I'd have to
rely on other parts of the application.

My question is: how would you solve this?


Jul 19 '05 #3


"Jahn Otto Næsgaard Andersen" wrote:

My question is: how would you solve this?


You are heading for a 'singleton'.
Try google to read more about it.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #4

"Jahn Otto Næsgaard Andersen" wrote:

[... lazy init stuff ...]
This works fine, but what should I do to make sure that sm_data is
deallocated when I close the application?


Refcounting and thread-sharing aside for a moment***, try this:

http://groups.google.com/groups?selm...30AF5%40web.de
(Subject: Re: inheritance and singletons)

regards,
alexander.

***) http://opengroup.org/austin/mailarch.../msg04858.html
Jul 19 '05 #5

"Jahn Otto Næsgaard Andersen" <sp*******@jotto.no> wrote in message
news:Kx*********************@juliett.dax.net...


This works fine, but what should I do to make sure that sm_data is
deallocated when I close the application?


Why don't you just delete it if it's not zero, and then set it to zero?
Jul 19 '05 #6
Jahn Otto Næsgaard Andersen wrote:
Hi,

I have a class A with a static pointer to some data. The pointer is static
because I want to access the data from all instances of the class. The data
itself is allocated when the first instance of A is constructed:

# a.h

class A
{
private:
static char* sm_data;


If you can replace this with

static std::string sm_data;

then do so. It will save you headaches, and solve your immediate problem.

Another alternative is to use a smart pointer here. Something similar to
std::auto_ptr should work, but auto_ptr itself is not appropriate,
because it will use 'delete' rather than 'delete[]'. You'd have to
define your own 'auto_array' or use one from an existing library.

Using a smart pointer would let the rest of the code remain basically
untouched, so it's a good "quick fix". However, quick isn't always the
best way. Your overall design would probably benefit from switching to
something that allocates and manages memory for you, such as the
std::string suggested above.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #7

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

Similar topics

29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
3
by: Ajax Chelsea | last post by:
can not the "static const int" be replaced by "static enum" anywhere? is it necessary that define special initialization syntax for "static const int"?
12
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one...
3
by: ruud.bos | last post by:
Hi list, As a C++ newbie, I have a question about static member functions. Suppose I have the following class definition: class MyClass { public: static void MyFunc(); };
9
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts...
11
by: comp.lang.php | last post by:
function blah($item) { if (!isset($baseDir)) { static $baseDir = ''; $baseDir = $item; print_r("baseDir = $baseDir\n"); } $dirID = opendir($item); while (($fyl = readdir($dirID)) !== false)...
9
by: Joseph Turian | last post by:
Consider this code snippet which doesn't compile: struct DebugOptions { }; class Debug { public: Debug(const DebugOptions options) { _options = options; } private:
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.