Reputation: 37
I need to know the difference,am beginner.
Upvotes: 2
Views: 29458
Reputation: 133
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.
Upvotes: -1
Reputation: 13843
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
Upvotes: 0
Reputation: 2114
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.
Upvotes: 0
Reputation: 21
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).
Upvotes: 2
Reputation: 234857
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.
Upvotes: 4
Reputation: 109
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.
Upvotes: 10
Reputation: 11
Upvotes: 1
Reputation: 361772
static
namespace scope variables have internal linkage, while non-static
namespace scope variables have external linkage by default! Details: a const
namespace scope variable has internal linkage by default. That linkage can by changed by keyword extern
.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.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.static
data member of a class has external linkage if the name of the class has external linkage. [$3.5/5]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.Upvotes: 3
Reputation: 180255
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 of static
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.
Upvotes: -1
Reputation: 34655
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.
Upvotes: 1