Pages: [1]   Go Down
Author Topic: difference between static int and int  (Read 584 times)
0 Members and 1 Guest are viewing this topic.
Pakistan
Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am little bit confused in the follwoing program.
Code:
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:
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?
« Last Edit: June 06, 2013, 05:09:57 am by arslankhan » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 644
Posts: 50512
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 100
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: June 06, 2013, 05:42:39 am by majenko » Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Cincinnati, OH
Offline Offline
God Member
*****
Karma: 49
Posts: 886
I'm not bossy...I just know what you should be doing.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pakistan
Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: