I want to carry out initialisation ONCE for use by a class.
I am using a dummy static variable dummy to do this. However, during my
initialisation code, I am allocating memory etc for various variables
used by the class.
Although I am not explicitly deallocating memory, alloc'd memory will be
returned to the heap when the app terminates - but I am not happy with
this. I am looking for a way to explicitly deallocate memory when there
are no more instances of the class that requires the new'd variables.
I can think of several ways of doing this - all of which seem a bit of a
hack (or maybe even wrong?). i'd like some comments on my ideas, and
pointers if any of my proposed solns are wrong or have side effects - or
preferably, a better way of solving this problem.
1). Use a class variable for reference counting and explicitly
"uninitialize" when last instance is dying
2). Wrap up all of the variables to be new'd during the initialisation
process, into a data type (struct or class), make a class variable of
that type, initialize it once, and deallocate in the new data types
destructor
Any more ideas ? 1 2690
On Sep 28, 8:46 pm, Anonymous <no.re...@here.comwrote:
I want to carry out initialisation ONCE for use by a class.
I am using a dummy static variable dummy to do this. However, during my
initialisation code, I am allocating memory etc for various variables
used by the class.
Although I am not explicitly deallocating memory, alloc'd memory will be
returned to the heap when the app terminates - but I am not happy with
this. I am looking for a way to explicitly deallocate memory when there
are no more instances of the class that requires the new'd variables.
I can think of several ways of doing this - all of which seem a bit of a
hack (or maybe even wrong?). i'd like some comments on my ideas, and
pointers if any of my proposed solns are wrong or have side effects - or
preferably, a better way of solving this problem.
1). Use a class variable for reference counting and explicitly
"uninitialize" when last instance is dying
2). Wrap up all of the variables to be new'd during the initialisation
process, into a data type (struct or class), make a class variable of
that type, initialize it once, and deallocate in the new data types
destructor
Any more ideas ?
struct foo {
~foo() { std::cout << "Destructed\n"; }
};
int main()
{
foo* f = new foo();
f->~foo();
std::cin.get();
return 0;
}
Cheers,
Chris Val This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: sks_cpp |
last post by:
When do static members allocate memory? (at load time)
How about static variables within a method?
Where is the memory for static variables - is it allocated on the heap or
does it have a...
|
by: Datta Patil |
last post by:
Hi ,
#include<stdio.h>
func(static int k) /* point2 : why this is not giving error */
{
int i = 10 ;
// static int j = &i ; /* point 1: this will give compile time error */
return k;
}
/*...
|
by: Atul Malaviya |
last post by:
>From a design/usability perspective.
When will one use a singleton pattern and when a class with purely
static members?
What are the pros and cons? I have inherited a code base which is full...
|
by: Joe Narissi |
last post by:
I know how to create and use static constructors, but is there a such thing
as a static destructor?
If not, then how do you deallocate memory intialized in the static
constructor?
Thanks in...
|
by: plmanikandan |
last post by:
Hi,
I have a doubt of storing static variables in memory.I think
static,global variables are stored in the same location.Static,Gloabal
variables are stored in HEAP .Can anybody explain me the...
|
by: Morgan Cheng |
last post by:
In C#, we can initialize static member variables in static constructor.
Now, when to destruct these static member variables? If these variables
hold external reference(e.g. log file stream), it is...
|
by: DaTurk |
last post by:
Hi,
I've noticed that when trying to create a managed, auto_gcrooted
variable in a antive class, the compiler throws linker exceptions.
While searching some forums I found out that this is a...
|
by: Marvin Barley |
last post by:
I have a class that throws exceptions in new initializer, and a static
array of objects of this type.
When something is wrong in initialization, CGI program crashes
miserably. Debugging shows...
|
by: Jorge Peixoto de Morais Neto |
last post by:
I was reading the code of FFmpeg and it seems that they use malloc
just too much. The problems and dangers of malloc are widely known.
Malloc also has some overhead (although I don't know what is...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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,...
|
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...
|
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: 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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |