Reputation: 2790
Can anyone please tell me when the static
variables/functions are allocated memory and in which memory segment? i.e. static global variable, static member variable, static local variable
etc all are initialized once before the beginning of program and all retains its values?
Furthermore, If Class MyClass
has static variable count
, when I declare MyClass obj
in main, then MyClass
object is created and count is given memory, If I declared MyClass obj2
, what happens in terms of memory? Is there any count
in obj2
which refers to the count
of obj1
? Or there is only separate memory allocation. Hope so that I am able to clearly ask the question.
Thanks in advance.
Upvotes: 3
Views: 1999
Reputation: 9680
The keyword static
has different meaning depending on the context it is used in. static
variables are allocated on the heap and their lifetime extends across the entire run of the program.
A static global variable is same as a global variable except that the static
keyword limits the scope of the variable to the file containing it only. This is called file linkage or internal linkage.
A static member variable is created when the class containing it is defined. This means it can be accessed using the class's name. This variable is shared across all instances of the class.
A static local variable has the same lifetime as that of a static global variable except that its scope is the immediate block containing it.
When a function is qualified with the static
keyword, this means that the scope of the function is the file containing it only. The function cannot be called by a function in another file. This is, again, called file scope or internal linkage.
Upvotes: 0
Reputation: 106122
When we declare a member of a class as static
it means no matter how many objects of the class are created, there is only one copy of the static
member. It exists even though no objects of the static data member's class exist.
A static
member is shared by all objects of the class.
If I declared
MyClass obj2
, what happens in terms of memory? Is there anycount
inobj2
which refers to thecount
ofobj1
?
Yes. There is only one count
for all objects. This test program would explain this a bit clear;
class Something
{
public:
static int s_nValue;
};
int Something::s_nValue = 1;
int main()
{
Something cFirst;
cFirst.s_nValue = 2;
Something cSecond;
std::cout << cSecond.s_nValue;
return 0;
}
output:
2
Because s_nValue
is a static
member variable, s_nValue
is shared between all objects of the class. Consequently, cFirst.s_nValue
is the same as cSecond.s_nValue
.
Furthermore, If Class
MyClass
hastatic
variable count, when I declareMyClass obj
in main, thenMyClass
object is created andcount
is given memory
No. In fact, count
exists even if there are no objects of the class have been instantiated!
Upvotes: 4
Reputation: 30156
The keyword static
can probably be seen as somewhat "overloaded".
The following usage-options are all viable:
In terms of runtime, all types of static variables are essentially the same. They all reside in the data-section of the program, and their addresses remain constant throughout the execution of the program. So the only difference between them is during compilation, in the scope of declaration:
In terms of runtime, all types of functions (static and non-static) are essentially the same. They all reside in the code-section of the program, and their addresses remain constant throughout the execution of the program. So the only difference between them is during compilation, in the scope of declaration:
Upvotes: 0