Static class members

O

Oliver Chong

Hi, I'm hoping some knowledgeable people can help me clear up a few
questions that popped in to my mind this morning regarding static
class members (aka class variables.)

If i have a static class member variable where does that variable
actually get allocated?

Also, at what point does the static member get allocated? Is it
created as soon as the process executes or when the first instance of
its member class is instantiated?

Lastly, does that static variable ever get deleted during execution or
only at process termination?

Thanks,
Oliver
 
V

Victor Bazarov

Oliver said:
Hi, I'm hoping some knowledgeable people can help me clear up a few
questions that popped in to my mind this morning regarding static
class members (aka class variables.)

If i have a static class member variable where does that variable
actually get allocated?

They have static storage duration (if they actually have storage).
The actual location of that static storage in memory is implementation
and platform specific. On many systems it's called "the data segment".
Also, at what point does the static member get allocated?

At the program initialisation.
Is it
created as soon as the process executes or when the first instance of
its member class is instantiated?

As the process executes.
Lastly, does that static variable ever get deleted during execution or
only at process termination?

The latter.

V
 
D

dheeraj.khajuria

hello,

I've tried to give some of the answers to your questions


Hi, I'm hoping some knowledgeable people can help me clear up a few
questions that popped in to my mind this morning regarding static
class members (aka class variables.)

If i have a static class member variable where does that variable
actually get allocated?

Static class member variable are allocated in initialized .data
section. note u have uninitialized section .bss also..

well, you can also check it using objdump or nm etc.. unix utility.
Also, at what point does the static member get allocated? Is it
created as soon as the process executes or when the first instance of
its member class is instantiated?

soon as the process executes..
Lastly, does that static variable ever get deleted during execution or
only at process termination?
only at process termination..
 
C

Chris Thomasson

[...]
Lastly, does that static variable ever get deleted during execution or
only at process termination?

IMHO, you should consider the case when a static object is allowed to create
and destroy other objects. Think of using a reference counted pointer as a
static object:

_____________
static refcount<foo> g_foo = new foo;

threads_a_y() {
for(;;) {
refcount<foo> l_foo = g_foo;
if (l_foo) {
l_foo->foo();
}
}
}

thread_z() {
for(;;) {
refcount<foo> l_foo = new foo;
g_foo.swap(l_foo);
}
}
______________


http://groups.google.com/group/comp.programming.threads/browse_frm/thread/e5167941d32340c6


?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,118
Latest member
LatishaWhy
Top