Re: Creating member vars on the heap, not the stack
JoeB wrote:
I have a class that is a singleton. There will only ever be one
instance, and it is in scope for the entire duration of the program.
Okay.
for this reason, i would like the member vars in the class to be stored
on the heap, not the stack.
Other than declaring all the members of the class as static, is there
another way?
Never do that. 'class-static' is something completely different than 'exists
for the whole runtime of the program' and it doesn't make sense in the
context of an object to externalize all its data.
The class its self cannot be declared as static, because that causes the
c'tor to be called before main(), which is a definite no-no.
In general, you can create objects before entering main(). If there are
special reasons not to, create the object itself on demand or explicitly
inside main() to solve this problem.
Uli
.
Relevant Pages
- Re: Passing more than 1 param in AfxBeginThread
... what is the difference between declaring the the pointer *p on the heap and then deleting after finishing with it, and just declaring it on the stack? ... If you declare a variable on the stack it disappears when the function ... (microsoft.public.vc.mfc) - Re: Passing more than 1 param in AfxBeginThread
... what is the difference between declaring the the pointer *p on the heap and then deleting after finishing with it, and just declaring it on the stack? ... What i want to know is when should i declare a var on the heap instead of on the stack? ... (microsoft.public.vc.mfc) - Re: dot net and stack
... allocated from stack - as opposed to from the heap. ... field, say, of value type is stored within the object BLOB. ... I suspect the author just did not consider the possibility of declaring a value variable in a module, but not in a class or procedure. ... (borland.public.delphi.non-technical) - Re: FindFirstFile Possible Memory Leak
... It's interesting what you say about adding to the stack during ... Declaring all variables at the top is an artifact of how programming ... instructions were setup. ... Interpreted languages were not quite so constrained as their only ... (microsoft.public.vb.general.discussion) - Re: How does managed code work?
... Does it work the same way as the native stack with a frame pointer that is the head of a linked list of stack frames where each time we enter a function we create a new stack frame in which new variables are pushed and each time we exit a function the entire stack frame is popped? ... Can someone point me to a discussion of the managed heap? ... How does it prevent memory leaks that occur in COM when two objects reference each other and keep the others reference count nonzero? ... Because objects don't go out of scope, ... (microsoft.public.dotnet.languages.csharp) |
|