Pages: [1]   Go Down
Author Topic: simple question about variable creation and initialization in loop()  (Read 873 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am very much an amateur with all things programming related so I am curious about the normal use of the static keyword when declaring a variable.  I have slightly more experience programming with avrstudio in C whereby in my main function I will first declare my variables and then use a 'while' loop to cycle through my code that actually has action items.  I notice that the equivalent process in arduino is to declare my variables all as static to avoid having the loop function create and initialize them on each iteration through my loop.  Is this pretty typical to declare all variables in loop as static if you don't want to reinitialize them every time?  For example it seems that:

Code:
void main()
{
int i = 0;
int j = 10;

while(1)
{
i++;
j--;
}
}

is not equivalent to:

Code:
void loop()
{
int i = 0;
int j = 10;

i++;
j--;

}

but is equivalent to:

Code:
void loop()
{
static int i = 0;
static int j = 10;

i++;
j--;
}

« Last Edit: October 08, 2011, 02:37:20 pm by jerseyguy1996 » Logged

Arduino Uno;
Mega328

Darkest Cheshire
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Unless you have an explicit need to declare local variables and make them static, it's more usual in Processing to declare them globally at the start of the code before setup().
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6106
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is this pretty typical to declare all variables in loop as static if you don't want to reinitialize them every time? 
Either declare them as static or declare them globally.

Your examples are functionally correct.

You might be interested in this thread:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1283329855

Turns out, void loop() is actually inside of an infinite for-loop.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 289
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the replies.  I guess I have always tried to limit the use of global variables only because I had read somewhere that it is poor form to declare lots of global variables.  Of course that was programming a simple application in C# so perhaps that doesn't apply here.
Logged

Arduino Uno;
Mega328

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8646
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void main()
    {
    int i = 0;
    int j = 10;

    while(1)
        {
        i++;
        j--;
        }
    }

is not equivalent to:

Code:
void loop()
    {
    int i = 0;
    int j = 10;

    i++;
    j--;
    }

because in the first example the variables never go "out of scope".  To write the equivalent main() you would write:

Code:
void main()
    {
    while(1)
        {
        int i = 0;
        int j = 10;

        i++;
        j--;
        }
    }
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

In this code:

Code:
while(1)
  {
  i++;
  j--;
  }

It doesn't really matter where the variables are declared. The loop will never exit.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

In fact the compiler is smart enough to realize that code doesn't do anything except loop, and optimizes away everything except the infinite loop:

Example code:

Code:
void setup()
{
  int i = 0;
  int j = 10;

  while(1)
  {
    i++;
    j--;
  }
}

void loop () { }


Generates for "setup" this single instruction:

Code:
void setup()
  a6: ff cf        rjmp .-2      ; 0xa6 <setup>

000000a8 <loop>:
    j--;
  }
}
Logged

Pages: [1]   Go Up
Jump to: