Você está na página 1de 24

4002-208 Intro. to Prog.

[in C++] Week 9 Day 1 Scope

Objectives
Storage Classes Scope Rules

4002-208

Storage Class and Scope


Storage class
Determines the period during which an identifier exists in main memory. Duration of program or created as needed.

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

Automatic Storage Class


Keywords auto and register are used to declare an automatic storage class. Automatic variables created when program execution enters the block in which they are defined. Automatic variables exist while the block is active. Automatic variables destroyed when the program exits the block.
4002-208 5

Automatic Storage Class


Applies only to
Local Variables Function parameters

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

Automatic Storage Class


Conserves memory use as the variables exist in memory only when the block in which they are defined is executing.

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

Static Storage Class


Keywords extern and static declare identifiers for variables and functions. Exist from the point at which the program begins execution and last for the duration of the program. Memory allocated at start of the program. Initialized only once.
4002-208 11

Static Storage Class


Does not mean that variable can be used throughout the program. Storage class and scope (where a name can be used) are separate issues.

4002-208

12

Static Storage Class


Two types: global and local Global
Created by placing variable declarations outside any class or function definition. Retain their value throughout the execution of the program. Can be referenced by any function that follows their declaration in the source file.
4002-208 13

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

Local Static Storage Class


Only known in function in which it is defined. Retain value when function returns to caller. Next time function is called, the variable contains the value it had when the function last completed. Declaration: static int numCalls = 0; Initialized to zero if not explicitly initialized. Good practice to initialize.
4002-208 16

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;

You are forbidden to use a goto period!


4002-208 19

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

Você também pode gostar