global vs local variables

why is it that most arduino example programs that I come across always declare global variables like this ...

long randNumber;

void loop() {
randNumber = random(10);
...
}

... and not like this ...

void loop() {
long randNumber;
randNumber = random(10);
...
}

Is it because of some kind of performance advantage?
Or does not doing it cause memory leaks?

I prefer local variables, but am worried that there's a good reason for using global variables like this instead.

On some architectures, globals may have a slight performance advantage, but generally, you should aim to keep scope as tight as possible.
I don't know why so many examples are written as they are; it irks me.

Local variables are stored on the stack and when the function is finished, the local variables are gone as well. Global variables always exist and use their memory during the life time of the entire program. There's a rule of thumb that says: always declare a variable nearest to where it's used. So local variables are to be preferred ...

kind regards,

Jos

mikgol:
why is it that most arduino example programs that I come across always declare global variables like this ...

long randNumber;

void loop() {
randNumber = random(10);
...
}

... and not like this ...

void loop() {
long randNumber;
randNumber = random(10);
...
}

Is it because of some kind of performance advantage?
Or does not doing it cause memory leaks?

I prefer local variables, but am worried that there's a good reason for using global variables like this instead.

Generally, if a variable is declared outside of a function, like the first example you gave, it is accessible to any function; it is of Global Scope.

If a variable is declared within a function like the 2nd example, it is accessible only within that function; it is of Local scope;

Global variables are persistent, they will be around forever.

Local variables will be destroyed once the function ends, unless they are declared static.

Maybe offtopic ,but it's a good idea to size your variables well(byte-0 to 255,etc)to save memory and other cases:EX

for(byte i=1;i<=10;i++)
//code here

instead of
byte i;
for(i=1;i<=10;i++)
//code here
The same rule is applied .
Hope it helped.

The Arduino has limited memory and limited program space compared to a PC. I see no reason to get precious about Global or Local variables.

To my mind Global variables make it easy to keep track of how much of the limited SRAM I am using.

In any case Static Local variables use just the same amount of memory as Global variables and simple local variables only save memory if there is a another function that is called later and which can put its local variables into the same registers. I don't think that can be true of loop() because it is the "father" function.

But perhaps I am just a programming phillistine.

...R

Take a look at #6:

http://forum.arduino.cc/index.php?topic=228630.5

Global variables save some (stack) memory as they never need to be passed as parameter, and therefore they might be a few clock ticks faster in use.

important difference to be made in microcontrollers is which global variables are const or changeable. The first group can possibly be stored in PROGMEM saving more valuable memory. I don't know if local variables that are const can be placed in PROGMEM, never tried.

And then there are volatile variables which cannot be optimized.

When I'm working on a new sketch I make all my new variables global to avoid the "not declared in this scope" error, then when the sketch is working correctly I go back and localize as many variables as I can, but many programmers (myself included sometimes) will "leave well enough alone" and leave it as is. Sloppy progamming and I'm as guilty as anyone. :frowning:

outsider:
When I'm working on a new sketch I make all my new variables global to avoid the "not declared in this scope" error, then when the sketch is working correctly I go back and localize as many variables as I can, but many programmers (myself included sometimes) will "leave well enough alone" and leave it as is. Sloppy progamming and I'm as guilty as anyone. :frowning:

Should work, but if it works why optimize?

Ever considered using a tool like cppcheck.exe to give you the right hints about scope.
It does a decent job.

Local variables are stored on the stack and when the function is finished, the local variables are gone as well. Global variables always exist and use their memory during the life time of the entire program. There's a rule of thumb that says: always declare a variable nearest to where it's used. So local variables are to be preferred ...

What if you use private and public variables? Is the private stored the same was a global is stored?

Ever considered using a tool like cppcheck.exe to give you the right hints about scope.
It does a decent job.

I'm looking at this right now, it seems interesting. Do you use it with an external editor or do you have to copy your code from the ide and run it past this program, then copy it back into the ide when finished?

Do you have any other advice regarding other debugging tools?

Do you use it with an external editor or do you have to copy your code from the ide and run it past this program, then copy it back into the ide when finished?

No, I run it from command line or from Jenkins.

Do you have any other advice regarding other debugging tools?

Brains, the most underestimated tool for debugging.

robtillaart:
Brains, the most underestimated tool for debugging.

How true.

...R