views:

345

answers:

7

Suppose I have a static variable declared inside a function in c. If I call that function multiple times, does the static variable get re-allocated in memory every time the function call? If it does, why the last value can always be maintained?

Example:

void add()
{
    static int x = 1;
    x++;
    printf("%d\n",x);
}

int main()
{
    add(); // return 2
    add(); // return 3
    add(); // return 4
}
+10  A: 

No - static variables are basically globals that live within the local namespace.

Aaron
+2  A: 

No,

How else should it retain it's value between function calls?

Static variables are like global variables in this regard, however they are local to the surrounding scope (function, class, namespace).

Johannes Rudolph
+4  A: 

No, the variable is not reallocated everytime. It is like having a global variable, but it only has local scope; it is global, but you can't only reference it from inside of that function.

f0ster
A: 

include

void foo() { int a = 10; static int sa = 10;

a += 5;
sa += 5;

printf("a = %d, sa = %d\n", a, sa); }

int main() { int i;

for (i = 0; i < 10; ++i)
  foo(); }

This prints

a = 15, sa = 15

a = 15, sa = 20

a = 15, sa = 25

a = 15, sa = 30

a = 15, sa = 35

a = 15, sa = 40

a = 15, sa = 45

a = 15, sa = 50

a = 15, sa = 55

a = 15, sa = 60

sikender
This is not an answer to this question, the question he asked does not require any code. He simply wants to know if the memory is being re-allocated.
Russ Bradberry
+2  A: 

In the C programming language, static is used with global variables and functions to set their scope to the containing file. In local variables, static is used to store the variable in the statically allocated memory instead of the automatically allocated memory. While the language does not dictate the implementation of either type of memory, statically allocated memory is typically reserved in data segment of the program at compile time, while the automatically allocated memory is normally implemented as a transient call stack.

sikender
+2  A: 

The static variable is not re-allocated every time the function is called. They act as normal local variables. But they differ in storage duration i.e. when a function assigns some value to the static variables, that value is retained between function calls.

Sailaja
+1  A: 

As the static prefix suggest the variable is in static memory that contains variables whose addresses is known at compile time (a somewhat pedantic way to say with global variables). That different from automatic variables (allocated on the stack) and dynamic variables (allocated on the heap using malloc).

The initialization of static variables in functions (or other static) is performed before the program is run. More precisely it means it can only be some constant expression, that the compiler can get at compile time.

That means the following program is not valid:

int f(int x){
    return x+1;
}

int main(){
    static int a = f(1);
    return a;
}

When I compile it with gcc it complains as expected with the following message:

error: initializer element is not constant

However when the program run you can change the value of static variables as any other one.

kriss