Community discussion forum
How do I Initialise / Clean up static variables?
-
I'm trying to work out if its possible for me to initialise / cleanup static variables without explicitly getting the main method of my program to initialise / cleanup for me.
Initialisation can be done fairly easily:class Foo {
static int * bar;
};
int * Foo::bar = new int;
But is there any way that I can calldelete Foo::barexplicitly at the end of my program?
thanks for your time
-
I think you dont quite understand the usage and requirements of static variables defined in classes. Once you define them, space is already allocated. You need to initialize them globally to some value eg. 0. Read any C++ book for more details.
-
If it (static pointer) is a public member of your class, you can free memory by 'delete class::static_ptr_member' outside the class scope. But if it's a private member, you can free memory by deleting that inside the class scope (in one of static member functions of class or in a friend function or you can return the static pointer from a static member function of class, as a non-constant_pointer then access that outside the class scope). Actually there is no difference on accessing, between static & non-static member functions except that static members exists till the end of program. (a pointer variable & memory that is allocated and its address is in that pointer, are distinct things).
-
tausif.ik, if you look at the example I gave, a call to "new" has been made, so a call to "delete" must also be made. Its a little pointless as code I'll admit, but it was just intended as a simple example.
Okay I've managed to do this. Its a dirty hack and I'm not sure its any more elegant than doing thingsd explicitly in the main method... but it works.
One warning to give with this code is that if you do this for more than one class, there is virtually no control over the order in which classes will be initialised, so you can't rely on any static members of other classes.////////////////
// myHeader.h //
////////////////
class Foo {
public:
static int * m_bar;
private:
class Initialiser() {
Initaliser();
~Initaliser();
}
friend class Initialiser;
static Initialiser m_initialiser;
}
////////////////
// myCode.cpp //
////////////////
#include "myHeader.h"
int * Foo::m_bar;
Foo::Initialiser Foo::m_initialiser;
Foo::Initialiser::Initialiser() {
Foo::m_bar = new int;
}
Foo::Initialiser::~Initialiser() {
delete Foo::m_bar;
}
Like I say this is a really dirty hack, but it works.
-
couling : Your Idea was COOL!

I used your idea, but my implementation is different.
Let me explain more about the story!
:- Problem : When you have a static pointer variable (like: static char* st_ptr) in your class, that you want to allocate some memory to that (during runtime with operator new), then at the end of program you want to delete that memory. How can you delete that at the end of program ?! Just using some way to access that memory at the end of main function, then delete it? But when you want to give your class to a customer, you should say to him : “Delete some of memory at the end of your main function, Thanks!”, terrible! really impractical! Then you should do that in some way in your class .
- Solution : Benefit of destructors , they’re called just at the end of something!
-
My Implementation is : define a static struct in your class (private or public), then add all of your static variables as non-static members of that struct, then all of those variables will become static variable members of your class (except, just use struct name in addition to class name, for accessing them). You can initialize those vars in the struct constructor, and don’t need to define them in the file scope (you just declare the struct itself : ‘class1::statics class1::st1;’ ).
-
Other Benefits : wrap up all the static vars in a struct, facilitate initializing static vars.
- NOTEs :
1.The constructor of structure will be called at the beginning of program and destructor will be called at the end of program (even if there is not exist any instances of the class, i.e., you don’t declared any variable of class type, since the structure will exist (constructed) from where it’s declared [class1::statics class1::st1;] to the end of program).
2.In VC++6 : destructor of structure may not work as you expect, then compile in VC++05.
-
Code :
#include <iostream>
using namespace std;
#include <cstring>
class class1
{
public:
static struct statics
{
int st_int;
char* st_ptr;
statics()
{
st_int = 35;
st_ptr = new char[20];
strcpy( st_ptr, "struct_String");
}
~statics()
{
delete st_ptr;
}
}st1;
};
class1::statics class1::st1;
int main()
{
class1 cl1;
cl1.st1.st_int = 43;
strcpy( cl1.st1.st_ptr, "main_String"); // don't use 'cl1.st1.st_ptr = "String"', because it will get a const-pointer, then when you want to delete that, you can't!
class1 cl2;
cout << cl2.st1.st_ptr << endl << cl2.st1.st_int << endl;
return 0;
}
-
Since the rule is that only static functions can access the static variables,my suggestion goes like this.
Write a static function which deletes the memory allocated by the static variable and call the function at the end of ur program.
class Foo
{
static int * bar;
public:
static void deleteStatic()
{
delete bar;
bar = NULL;
}
};void main()
{
.....
......
Foo::deleteStatic();
}
-
sjith2000 : No problem with your code, but please see the ' Problem ' section in my previous post, then you'll know why to avoid your way.

-
Hi Rastkar,
I saw your program.
In your case, the destructor of the class class1 will be called twice when the program ends, since you created 2 objects - cl1, cl2.But the destructor of the struct statics will never be called, which won't free the memory allocated by st_ptr .
~statics()
{cout<<"This won't be printed on the screen";
delete st_ptr;
}
-
Hi sjith2000,
Cons\Dest, of class1 itself, will be called for any instance of that, and it's a normal act (I've not defined any of cons\dest for class1) , but for the 'structure', cons will be called at the beginning of the program and dest at the end, both of them just once.
[quote user="sjith2000"]
... But the destructor of the struct statics will never be called, which won't free the memory allocated by st_ptr . ...
[/quote]
I mentioned : with VC++6 compiler you have some problems, one of them is that you said! I don't know what compiler you've used, but with VC++ 2005 , cout<<"This won't be printed on the screen"; will be printed on the screen!
. -
THE CORRECT WAY !
class CClass
{
public:
CClass(void);
~CClass(void);
private:
static int *Pointer;
static int Ref; /*** count references to the Pointer ***/
};
int CClass::Ref = 0;
int *CClass::Pounter = NULL;
CClass::CClass(void)
{
++Ref;
if(Pointer==NULL)
Pointer = new int;
}
CClass::~CClass(void)
{
--Ref;
if(Pointer && Ref==0)
{
delete Pointer;
Pointer = NULL;
}
}
Sorry for the very bad formatting... This Forum Auto added all those extra spaces !
-
qwijibow :
It's an easy (and good) way, but not automatic one.
It uses one more static variable and more operations. -
But it works properly with VC++ 6 complier also.Can u pls explain what you meant by automatic.
-
by automatic, I mean : at the end of your program, the compiler itself will call the structure's destructor and you don't need to do anything to know when the last instance of your class is being destructed then delete memory (memory will be deleted automatically).
Post a reply
Related discussion
-
Help: MS VC++6 crash!!
by Yodin Mohd (8 replies)
-
help me with a problem anybody?
by Ru$$ (2 replies)
-
Junior Programmer wanted - Middlesbrough
by stacey moore (0 replies)
-
Getting the version of Word used to create a document (C#)
by James Crowley (1 replies)
-
How to implement remoting in our web application(ASP.NET with C#)
by James Crowley (1 replies)
Quick links
Recent activity
- Version Smith replied to How to convert DVD and vide...
- Version Smith replied to How to transfer or copy so...
- Version Smith replied to How to transfer ipod nano 5...
- Lovely Liezl Lomibao replied to bar graphs in visual basic.net
- Lovely Liezl Lomibao replied to bar graphs in visual basic.net
- Chongkun Zhu replied to How to Download and Convert...
Enter your message below
Sign in or Join us (it's free).