Go Down

Topic: simple question about variable creation and initialization in loop() (Read 976 times) previous topic - next topic

jerseyguy1996

Oct 08, 2011, 09:35 pm Last Edit: Oct 08, 2011, 09:37 pm by jerseyguy1996 Reason: 1
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: [Select]
void main()
{
int i = 0;
int j = 10;

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


is not equivalent to:

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

i++;
j--;

}


but is equivalent to:

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

i++;
j--;
}


Arduino Uno;
Mega328

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().

James C4S

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.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

jerseyguy1996

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.
Arduino Uno;
Mega328

johnwasser

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

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


is not equivalent to:

Code: [Select]
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: [Select]
void main()
    {
    while(1)
        {
        int i = 0;
        int j = 10;

        i++;
        j--;
        }
    }
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Nick Gammon

In this code:

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


It doesn't really matter where the variables are declared. The loop will never exit.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

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: [Select]
void setup()
{
  int i = 0;
  int j = 10;

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

void loop () { }



Generates for "setup" this single instruction:

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

000000a8 <loop>:
    j--;
  }
}
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up