Static and Automatic Variables
Automatic variable: memory is allocated at block entry and deallocated at block exit
In C++, a block is statements within curly brackets (functions, loops, etc.)
By default, variables declared within a block are automatic variables
Static variable: memory remains allocated if the program executes
Global variables declared outside of any block are static variables
Syntax:
static dataType identifier = value;
Static variables declared within a block are local to the block have the same scope as any other local identifier in that block.
Example use of a static variable
cpp
double runningTotal(double numberToAdd)
{
static double total = 0; // declare and initialize variable that
// remains over the whole program run.
total += numberToAdd; // add number to total
return total; // return updated total
}
cpp
double num = 0;
num = runningTotal(1); // num = 1.0
num = runningTotal(2); // num = 3.0
num = runningTotal(2); // num = 5.0
Question: What would happen if the variable was not static
?
Answer: num
would always equal the argument passed into the function:
cpp
num = runningTotal(1); // num = 1.0
num = runningTotal(2); // num = 2.0
num = runningTotal(2); // num = 2.0