Scope and Lifetime of Global and Local Variables
Scope and Lifetime of Global and Local Variables The contents of this lecture prepared by the instructors at the University of Manitoba in Canada and modified by Dr. Ahmad Reza Hadaegh A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 1
Scope and Lifetime - The scope of a variable is the region in which it is accessible in a program - we have seen many examples of scope already void main() { int x; . . } void foo() { x= 5; } - illegal: the scope of x is only the function main x is undefined in foo A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 2
Scope and Lifetime: Global - Variables generally global or local in scope - Global variables: – declared outside any function in program file ofstream fout; void main() { fout. open("outfile. txt"); . . . ; } void foo() { fout << "Print this to outfile. txt"; } - fout is declared globally - it is accessible to any function in the program file A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 3
Scope and Lifetime: Global - Usually only make input/output streams and constants global const float Pi= 3. 14159; const int Array. Size= 1000; ifstream fin; ofstream fout; void main() {. . . } void foo() {. . . } - all constants and globals are accessible in main and foo A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 4
Scope and Lifetime: Global - const globals should be used whenever a literal is used which most probably not going to be changed - this makes your program much more - readable: the reader is not always attempting to figure out what a literal is for - changeable: need only change the literal once at top (instead of throughout program) A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 5
Scope and Lifetime: Global - The lifetime of a variable is the portion of program execution during which the variable has memory allocated to it - the time during which the variable exists - the time during which values stored to a variable remain stored A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 6
Scope and Lifetime: Local - Local variables are those declared as - local variables in a function (including main) - formal parameters in a function - any local variables in any block: {. . . } - A block of code is the code between {. . . } - the body of any function is a block void printabar() { cout << "********" << endl; Code } Block A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 7
Scope and Lifetime: Local - Any block can have local variables void strange( int x ) { Block of function strange: int y; Block includes x, y y= x; if (y== 3) { Block between {. . . } int z; Block includes z z= y; cout << x << “ ”<< y <<“ ” << z; } } < See: Example 2> A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 8
Scope and Lifetime: Local - All local variables are enclosed in some block of code - The scope of a local variable is from the point of declaration to the end of the block - remember, variables don't need to be declared at the top of a block (but probably should be. . . ) void foo( int x) { cout << x; int y; x= y; cout << y; Scope of y } A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 9
Scope and Lifetime: Local - The lifetime of a local variable or parameter: - Begins when execution of code enters the block containing the declaration - Ends when execution leaves the block of code containing the declaration - Local variables and parameters are called automatic variables - memory is automatically allocated when the block is entered - memory is automatically reclaimed when the block is exited A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 10
Scope and Lifetime: Local - Consider the following situation float exp( float x) { int y; if (x>- 0. 5 && x< 0. 5) { for (y= 0; y< 10; y++) {. . . . } } } Block A Block B - y not declared in the block B of function exp – but y is certainly accessible within block B A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 11
Scope and Lifetime: Local - The variable y is non-local to Block B - y is defined as local in a block containing block B - Generally, a non-local variable for a block of code is either - a global variable (which are non- local to all blocks of code) - a local variable defined in a block containing the block under consideration - Global, non- local, and local variables are generally accessible in any block contained in the block in which they are defined A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 12
Scope and Lifetime const float pi= 3. 14159; // an approximation int main() { const float pi= 22. 0 / 7. 0; // in elementary school { const int pi = 3; // in the state of Indiana cout << pi; } } - only "const int pi = 3" is visible at the cout statement A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 13
Scope and Lifetime - In this case, the innermost definition of the variable pi is the one used by cout - this is called name precedence - when a variable name is used in a statement, it refers to the definition of that variable in the smallest or innermost block containing the statement A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 14
Scope and Lifetime void func 1 (int, char&); void func 2( ); int a 1; char a 2; int main() {. . . . } void func 1 (int a 1, char& b 2) { int c 1; int d 2; // variables used are : a 1 a 2 c 1 d 2 b 2 } What is the scope of each variable used in func 1? A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 15
Scope and Lifetime void func 2(); int a 1; char a 2; // repeat definitions for convenience void func 2() { int a 1; int b 2; while (. . . ) { int c 1; int b 2; } } - What is the scope of each variable used in func 2? A. R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 16
- Slides: 16