Escolar Documentos
Profissional Documentos
Cultura Documentos
Objectives
Storage Classes Scope Rules
4002-208
Scope
Determines the portion of the program where an identifier can be referenced.
4002-208
Storage Classes
An identifier's storage class determines the period during which that identifier exists in memory. Two storage classes
Automatic Static
4002-208
4002-208
Example
double doIt(double d1, double d2) { double sum = d1 + d2; double diff = d1 d2; return sum * sum + diff * diff; } Which are local variables?
4002-208 7
Example
double doIt(double d1, double d2) { auto double sum = 0.0; sum = d1 + d2; auto double diff = d1 d2; return sum * sum + diff * diff; } Keyword auto rarely used as it is the default.
4002-208 8
4002-208
Register Keyword
Suggests that compiler maintain the variable in one of the computer's highspeed hardware registers. Eliminates overhead of repeatedly loading the variable from memory into the register and storing the results back into memory. Used for highly-referenced variables. Used only with local variables and function parameters.
4002-208 10
4002-208
12
Example
static int count = 0; int main( ) { count = count + n; } int doIt(int m) { count -= m; }
4002-208 14
BE CAREFUL!!
Global scope allows too many parts of the program to alter the value. Debugging is difficult. Used to avoid "effort" of parameter passing. You are forbidden from using global declared variables.
4002-208
15
Example
void doSomething(double x, int n) { //count number of times function called static int numCalls = 0; numCalls++; cout << "Call number: " << numCalls; }
4002-208 17
Scope Rules
Portion of a program where an identifier can be used. Look at 4 types (there are two others):
Function scope File scope Block scope Function-prototype scope
4002-208
18
Function Scope
Labels are only identifier with this scope Label followed by colon (":") Example:
{ start: goto start;
File Scope
Identifier declared outside any function or class. Known in all functions from the point at which it is declared until the end of the file. Global variables, function definitions, and function prototypes placed outside a function have file scope.
4002-208
20
Block Scope
Declared inside a block. Begins with identifier declaration and ends at terminating right brace } Example
{ // sum not know here int sum = 0; // sum known here } // sum not known after here
4002-208 21
Nested Blocks
{ int temp = 0; // temp usable here in outer block to its end { double temp = 99.99; // double temp usable here // int temp not usable here "hidden" until inner block terminates } // double temp out of scope here // int temp usable here to end of block }
4002-208 22
Notes
Local variables declared as static still have block scope, even though they exist from the time the program begins execution. Storage duration does not affect the scope of an identifier.
4002-208
23
Function-prototype Scope
Only affects those identifiers used in the parameter list of a function prototype. Function prototypes do not require names in the parameter list only types required. Names appearing in parameter list of a function prototype are ignored by compiler. Names appearing in the parameter list of a prototype can be reused with ambiguity.
4002-208 24