where to declare variables and mem impact

Hi all!

While I have some background in C/C++ I am relatively new to embedded programming thus now need to pay more attention to memory utilization.

My main question regards where to declare certain variables and what the impact on memory overhead is...

If I declare variables inside the functions that use them, is this memory "free" when that function exits? i.e. is it a good practice to declare as many variables as possible within functions so as to maximize your available memory outside the function scope? If this is correct, what is the downside of declaring variables within a function? (other than of course not being able to access them globally)

I'd also appreciate any insight into how the "volatile" keyword plays into this... thanks!

You should always declare variable as close to where you want to use them, remembering that some variables may need global or static scope to preserve their values between calls to their functions. Some of the examples given in the learning section are, IMHO, quite poor, in that they give almost all variables global scope when they really don't need it.

"voltatile" is a hint to the compiler that the value of the variable may change outside the apparent program flow, either as a result of a variable changing because of an interrupt, or say, the value in a memory-mapped register changing because of some external event.

If I declare variables inside the functions that use them, is this memory "free" when that function exits?

Yes, such local variables are stored in the 'stack' and are freed when the function returns.

i.e. is it a good practice to declare as many variables as possible within functions so as to maximize your available memory outside the function scope? It really depends on your application. There is limited SRAM space in microcontrollers and if your sketch is pushing the limit then the fewer global variables and arrays you use the better. However if you are not up against a limit then it doesn't matter. If this is correct, what is the downside of declaring variables within a function? (other than of course not being able to access them globally) Maybe a little speed hit retrieving and storing variables on the stack vs global access.I'd also appreciate any insight into how the "volatile" keyword plays into this... thanks!

Volatile I don't think applies to locally defined variables defined inside functions.

I believe volatile forces a function to reread the variable directly from memory rather then use a local copy that was placed on the stack when the function was first called.

Microcontrollers by there nature of limited memory resources relies on the use of global variables and arrays more then traditional processors. So while most C/C++ language guides discourage overuse of global variables, microcontrollers tend to have to balance that advice against the resource limits.

I'm more hardware orientated, but that's my take on the advice and code examples I have come across on this forum.

Lefty

Oops, I meant to write:

voltatile" is a hint to the compiler that the value of the variable may change outside the apparent program flow, either as a result of a variable changing because of an interrupt, or say, the value in a memory-mapped register changing because of some external event.

…and that accesses to the variable should not be optimised out.
For example:while (myFlag != 0) ;
Here, the compiler has no reason to suspect that the value of “myFlag” could ever change, so could load the value once, test it, and loop forever or not loop at all based on that single snapshot.
However, by declaring “myFlag” as “volatile”, the compiler will reload the value repeatedly before each test.

Excellent stuff... thanks everyone for the responses! this really helps!