Go Down

Topic: difference between static int and int (Read 685 times) previous topic - next topic

arslankhan

Jun 06, 2013, 11:45 am Last Edit: Jun 06, 2013, 12:09 pm by arslankhan Reason: 1
I am little bit confused in the follwoing program.
Code: [Select]
void myfunction(){  
int my_stack=0;
static int my_static=0;

Serial.print("my_static=");
Serial.println(my_static);

Serial.print("stack=");
Serial.println(my_stack);
my_stack++;
my_static++;
}

my_static variable, declared as static within a function, retains its value between successive calls to my_function() while my_stack does not.  The output of 5 successive calls to my_function() is
Code: [Select]
my_static=0
stack=0
my_static=1
stack=0
my_static=2
stack=0
my_static=3
stack=0
my_static=4
stack=0


every time when myfunction() runs it comiles static int my_static=0; then how my_static can be increased?

PaulS

Quote
every time when myfunction() runs it comiles static int my_static=0; then how my_static can be increased?

No. When the code runs, nothing is compiled.

When the code is compiled, the compiler notices the static keyword in front of my_static and generates code to initialize the variable ONCE. The compiler notices the lack of the static keyword in front of stack, and generates code to initialize the variable every time.

majenko

#2
Jun 06, 2013, 12:38 pm Last Edit: Jun 06, 2013, 12:42 pm by majenko Reason: 1
When you define a local variable as static and equate it to something you are telling it the starting value only.  This value only gets assigned once.

It is functionally the same as a global variable, except the scope (where you can access it from) is more limited.

The code to assign the value to the static variable is run during the startup phase (not the setup function - before even that runs) - a process called "crt0" for "C RunTime phase 0".  This copies all the values for the pre-set (static and global) variables from flash into RAM, and that is the only time these initialization values are ever assigned.

In effect the "=0" of your static declaration is not performed there, but right at the start of the program before even setup() runs.

econjack

Your definition of my_static tells the compiler several things about the variable:

1. Because it is defined within myfunction(), my_static "lives" and is only visible within the opening and closing braces of myfunction(). That is, my_static has local scope within myfunction().
2. Because your definition of my_static is prefaced with the keyword static, it's as though the compiler removes the line of code that defines my_static out of myfunction() and places it before setup(). This means the definition is only read at program startup. This causes my_static to get a memory address (i.e., an lvalue) where it will "live" for the entire running of the program and initializes its value (i.e., its rvalue) to 0.

Therefore, when the code enters myfunction() the first time, my_static becomes visible (it's in scope). When the statement my_static++ is executed, the compiler fetches the rvalue from memory (i.e., 0) and increments it (rvalue now equals 1). Because you used the static keyword, my_static is not reallocated each time myfunction() is called. When the second call is performed, the same lvalue is used to fetch the rvalue (which is now 1) and it is again incremented (i.e., rvalue =2). This is why static variables "remember" their previous values. In short, your code is doing exactly what a static variable should do: get allocated once before the program starts and never get reallocated again. Because its lvalue is never reassigned, its rvalue is retained between function calls.

arslankhan

Thanks to all specially econjack ..thanks a lot. Well explanation from every one

Go Up