what is the difference between static and normal variables in c++?
I need to know the differen开发者_如何学运维ce,am beginner.
void func()
{
static int static_var=1;
int non_static_var=1;
static_var++;
non_static_var++;
cout<<"Static="<<static_var;
cout<<"NonStatic="<<non_static_var;
}
void main()
{
clrscr();
int i;
for (i=0;i<5;i++)
{
func();
}
getch();
}
The above gives output as:
Static=2
Nonstatic=2
Static=3
Nonstatic=2
Static=4
Nonstatic=2
Static=5
Nonstatic=2
Static=6
Nonstatic=2
Static variable retains its value while non-static or dynamic variable is initialized to '1' every time the function is called. Hope that helps.
A static variable is a single memory location associated with the class.
A non-static variable (that is a member of a class) represents a different memory location for each instance of the class.
Static variables can be initialized only once and assign to 0 when an object is created.
static
namespace scope variables have internal linkage, while non-static
namespace scope variables have external linkage by default! Details: aconst
namespace scope variable has internal linkage by default. That linkage can by changed by keywordextern
.static
variables in a class is associated with the class, that means, all instances of the class have the same instances of the static variables; they’re like a global variables that every instance of the same class has access to.- non-
static
variables in a class are instance members, that is, every instance of the class will have its own instances of the non-static
variables. - a
static
data member of a class has external linkage if the name of the class has external linkage. [$3.5/5] - a
static
variable inside a function retains its value even after returning from the function. That is, its lifetime is equal to the lifetime of the program itself. This is demonstrated in Mahesh's answer.
Main difference in static and normal variable is in their lifetime, for example scope and lifetime of local variable is within the function-loop in which it is declared, but scope of static variable is same as local variable means it will be accessed within which function it is declared(if not defined globally), but lifetime is through the program. So memory allocation depends on the lifetime, so as static variable will not die till program terminates so no new memory allocated, so fixed memory address allocated to static variables and value in that address will be overwritten every time we change the value of variable, while for normal variable as soon as you will go out of scope, variable will die(means memory will be freed for that variable) and when you will define variable again new memory address will be assigned, new value will be stored in address and no concept of overwrite(when we go out of scope).
Static variable retains its value during function calls/loops but local variable doesn't;
#include <iostream>
void foo()
{
for( int i=0; i<5; ++i )
{
static int staticVariable = 0;
int local = 0;
++local;
++staticVariable;
cout << local << "\t" << staticVariable << "\n";
}
}
int main()
{
foo();
return 0;
}
Results:
1 1
1 2
1 3
1 4
1 5
When a static variable is a member of a class, each instance share the static variable. Each instance doesn't have it's own copy.
class foo
{
public:
static int staticVariable;
};
int foo::staticVariable = 0;
foo obj1, obj2 ; // Both the instances share the static variable.
- Static members are members that are single shared member common to all the objects created for a particular class but non-static are not so.
- Memory allocation is done only once and not every time an object is created like non-static members.
- Only one copy of static data member will exist irrespective of the number of objects created.
- Static member functions can access only static member variables while a non-static member can be accessed by both static and non-static member functions.
- Static members are efficient when single copy of data is enough.
Apart from differences in all these answers, there is one more difference between static
and local
variables:
local
variables are stored on the stack, while static
variables are stored in the data
section of a process memory.
suppose class A
has static variable x... and not static variable p
now if you create hundred instance of class A (i.e A a;
) x would be shared among this hundred instance... but there would be hundred copies of p... one copy of p per instance of A
There are three different types of "static" variables.
Outside functions and classes, a "normal" variable would be a global variable. A
static
variable outside a function is not global, but local to the .cpp file in which it's defined. (Don't define this type ofstatic
variables in header files!)Inside a function, a normal variable is destroyed when the function exits. A
static
variable in a function retains its value even after the function exits.Inside a class, a normal member belongs to an object. A static member is shared between all objects of that type, and even exists before any object of that class is created.
The main Difference between "Static Variable" and the "Local variable"
The memory allocation of the local variable is deallocated if their work is done, on other hand, the Memory allocation for the static variable remains the same until the program terminates or overwritten by the another value of the program.
For example, Suppose You are using a recursion problem, If you are using the local variable during the recursive call, the memory allocation of the Local variable is removed, during the returning of the function.
On the other hand, if you are using the static variable, the memory allocation of that variable will remain until the program ends. It shall intact its values during the returning of the function, or can be overwritten by the programe.
精彩评论