static member variables

R

Rahul

Hi Everyone,

It is known that static member variables can be accessed without
creating an instance of the class and it serves as a global variable
without polluting the global name space.

So does that mean that the memory of the variable is allocated in
BSS (data segment meant for static global variables) when the program
is loaded and is available until the program is killed.

Thanks in advance!!!
 
Y

yanlinlin82

Hi Everyone,

It is known that static member variables can be accessed without
creating an instance of the class and it serves as a global variable
without polluting the global name space.

So does that mean that the memory of the variable is allocated in
BSS (data segment meant for static global variables) when the program
is loaded and is available until the program is killed.

Thanks in advance!!!

The life cycle of static member variables is same as the program. But
the order of them are not determinate. I thought that whether they are
allocated in BSS depends on the compiler.
 
R

Rolf Magnus

Rahul said:
Hi Everyone,

It is known that static member variables can be accessed without
creating an instance of the class and it serves as a global variable
without polluting the global name space.

So does that mean that the memory of the variable is allocated in
BSS (data segment meant for static global variables) when the program
is loaded and is available until the program is killed.

The variable is destroyed after main() finished its execution. It has static
storage duration, so it is handled like other variables with static storage
duration (i.e. like global variables). How that is done is beyond the scope
of the C++ standard. It's handled by the compiler. g++ usually puts them
into the .bss or .data section depending on the type and initialization
value.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top