Just to refresh your memory, a local variable is a variable that is created inside a block of code, between opening and closing curly brackets. A local variable is only accessible from within that block. I.e.
{
int var;
}
The variable called var is a local variable and it exists only while that block is executing. In other words, a local variable is created upon entry into a block and destroyed upon exit.
Local variables are most commonly created within blocks of code called functions. Here is one:-
void f1() {
int var;
}
The variable created inside the f1 function called var is a local variable. It is local to the function f1. It is directly accessible only within f1. Any other function that tried to access it as so..
void f2() {
var = 0;
}
...would produce a compiler error, informing you that var inside f2 is not declared. This is perfectly correct because var does not exist outside f1. As far as f2 is concerned var does not exist at all - ever.
We say that the scope of var is f1. It is created in memory when f1 is created. It exists only as long as f1 is executing. It is destroyed (deallocated) from memory at the same time that f1 is.
Now to be clear about the lifetime of a local variable, consider the following code:-
void main() {
{
int var;
}
var++;
}
This would produce a compiler error saying that var is undeclared. This is actually true because var is created on entry into the inner opening curly brace and destroyed upon exit. We cannot increment it outside it's block, since it no longer exists.
Here is another common mistake:-
void main() {
for (int x=0; x < 10; x++) {
int y = x;
}
cout << y;
}
This would produce a compiler error saying that y is undeclared. This is correct because y is declared inside the for block and so does not exist outside of it. It is created and destroyed inside the for block. In fact, y is created and destroyed 10 times in all.
Here are a few points to consider:-
-
One advantage of local variables is that memory will only be allocated to them as needed.
-
Most programmers declare all their local variables at the top of a function. This has the advantage of making code easier to read and maintain. However, you may declare a local variable anywhere within the function block.
-
Sometimes it is good practice not to declare a variable at the top of a function. For example, if a variable will only be used inside an if block, then declare it inside the if block. This saves memory because it will be destroyed on exit from the if block and not when the function ends.
-
Another advantage of local variables is that since they do not exist outside their block, they cannot be accidentally altered from somewhere else outside the block.
-
Finally you must remember that since a local variable only exists within it's block when the block ends, the variable is destroyed and it's value is lost. Consider the following code:-
void main() {
int res;
do {
res = f ();
cout << res;
}while (res < 10);
}
int f() {
int count = 0;
return ++count;
}
This code would produce an infinite loop because count is created and destroyed every time the function f1 is called. Since count is initialized to 0 every time, then the do-while loop never stops since res always has a value of 1 and never reaches 10.
This could be fixed a number of ways. By declaring count as a global variable or alternatively by declaring count as a static local variable.
A static local variable retains its value between function calls. It is not destroyed when the function exits. The one difference between a static local variable and a global variable is that you still cannot refer to that variable directly from a different function, so it is safer than using a global variable.
Let's have a look at the changed code:-
int f() {
static int count = 0;
return ++count;
}
Now count retains it's value between calls to f1 and so eventually reaches a value of 10;