Computer Science 1620 Lifetime Scope l Variable Lifetime
Computer Science 1620 Lifetime & Scope
l Variable Lifetime l a variable's lifetime is finite l Variable creation: l l l memory is allocated to the variable occurs at declaration Variable destruction: l l memory is returned to the program (for use with another variable) occurs at ….
l Variable l Destruction a local variable (the kind we are used to) lives until the end of its statement block this may be the entire function l this could also be a compound statement l l when a variable is destroyed its memory is no longer reserved – it may be used by another variable l the variable can no longer be used l
l Variable Lifetime - Example #include <iostream> using namespace std; int main() { int x; x = 2; return 0; Variable x's lifetime } End of statement block that x was declared in.
l Variable Lifetime - Example #include <iostream> using namespace std; int main() { int num; cout << "Please enter a number" << endl; cin >> num; if (num < 0) { int neg = -num; cout << "|" << num << "| = " << neg << endl; } Variable num's lifetime return 0; } End of statement block that num was declared in.
l Variable Lifetime - Example #include <iostream> using namespace std; int main() { int num; cout << "Please enter a number" << endl; cin >> num; if (num < 0) { int neg = -num; cout << "|" << num << "| = " << neg << endl; } Variable neg's lifetime return 0; } End of statement block that neg was declared in.
l Variable Lifetime – Loops l if a variable is declared inside the loop statement, then its lifetime lasts until the end of that loop statement l l this means that the variable is (theoretically) created over and over again if a variable is declared in the initialization of a for loop, then the variable's lifetime lasts until the end of the loop l this means that the variable is not redeclared over and over
l Variable Lifetime - Loops #include <iostream> using namespace std; int main() { for (int a = 0; a < 10; a++) { int b = a * a; cout << b << endl; } return 0; } a lasts from loop initialization until the loop has finished executing. b lasts from declaration until the loop statement has been executed.
l Variable Scope l l the area of a program where a variable can be used the scope of a variable is anywhere in the program where: l l l the variable is alive (between its creation and destruction) the variable is not being hidden by another variable of the same name if you try to use a variable outside of its scope, a compiler error results
l Variable Scope - Example #include <iostream> using namespace std; int main() { int x; x = 4; return 0; Variable x's lifetime } This code is fine … variable x is being used within its lifetime.
l Variable Scope - Example #include <iostream> using namespace std; int main() { x = 4; int x; return 0; Variable x's lifetime } This code generates a compiler error, since we are trying to use x outside of its scope.
l Variable Scope - Example #include <iostream> using namespace std; int main() { if (3 < 4) { int x = 5; Variable x's lifetime } cout << "x = " << x << endl; return 0; } This code generates a compiler error, since we are trying to use x outside of its scope.
l Variable Scope - Example #include <iostream> using namespace std; int main() { int x = 0; if (3 < 4) { x = 5; Variable x's lifetime } cout << "x = " << x << endl; return 0; } This code is fine … x is being used within its lifetime.
l Variable Scope - Example #include <iostream> using namespace std; int main() { if (3 < 4) { int x = 5; cout << "x = " << x << endl; } else { x = 2; cout << "x = " << x << endl; } Variable x's lifetime return 0; } This code generates a compiler error, since we are trying to use x outside of its scope.
l Variable Scope - Example #include <iostream> using namespace std; int main() { for (int x = 0; x < 10; x++) { cout << "x = " << x << endl; } Variable x's lifetime return 0; } This code is fine … we are using x within its lifetime.
l Variable Scope - Example #include <iostream> using namespace std; int main() { for (int x = 0; x < 10; x++) { cout << "x = " << x << endl; } cout << "x = " << x << endl; Variable x's lifetime return 0; } This code generates a compiler error, since we are trying to use x outside of its scope.
l Variable Scope - Example #include <iostream> using namespace std; int main() { for (int x = 0; x < 10; x++, y++) { int y = 2 * x; Variable cout << "y = " << y << endl; } y's lifetime return 0; } This code generates a compiler error, since we are trying to use y outside of its scope.
l Nested l Code Blocks we have seen code blocks nested inside other code blocks l e. g. if statements int main() { int x, y; cout << "Please enter two integers: "; cin >> x >> y; if (x < y) { if (y == 10) { cout << "x must be less than 10" << endl; } y = 0; } return 0; }
l Nested l Code Blocks we have seen code blocks nested inside other code blocks l e. g. loop statements int main() { int x, y; cout << "Please enter two integers: "; cin >> x >> y; while (x < y) { while (y < 10) { y++; } x++; } return 0; }
l Nested l Code Blocks we have seen code blocks nested inside other code blocks l standalone compound statements int main() { int x, y; cout << "Please enter two integers: "; cin >> x >> y; { { y++; } x++; } return 0; }
l Variable Declaration until now, we have said that you cannot declare two variables of the same name in the same function l this is actually stronger than the actual rule l l you cannot declare two variables of the same name in the same statement block
l Variable Declaration - Example #include <iostream> using namespace std; int main() { int a = 10; cout << a << endl; int a = 9; cout << a << endl; return 0; } This code generates a compiler error, since we are trying to declare two variables with the name a inside the same statement block.
l Variable l Declaration two variables can have the same name if they are declared in different statement blocks #include <iostream> using namespace std; int main() { int a = 10; cout << a << endl; { int a = 9; cout << a << endl; } return 0; }
l Variable l Declaration two variables can have the same name if they are declared in different statement blocks #include <iostream> using namespace std; int main() { int a = 10; cout << a << endl; if (3 < 4) { int a = 9; cout << a << endl; } return 0; }
l Variable l Lifetime two variables can have the same name if they are declared in different statement blocks #include <iostream> using namespace std; int main() { int a = 10; cout << a << endl; if (3 < 4) { int a = 9; cout << a << endl; } Overlap return 0; } First variable a's lifetime Second variable a's lifetime
l Variable l two variable's with the same name may have overlapping lifetimes l l Lifetime one of the variables is declared in a nested statement block Question: if the variable name is used inside the overlap area, which variable is being referred to?
l Variable l Lifetime two variables can have the same name if they are declared in different statement blocks #include <iostream> using namespace std; int main() { int a = 10; cout << a << endl; if (3 < 4) { int a = 9; cout << a << endl; } return 0; } Does this refer to the first or the second a?
Rule: When two variables have overlapping lifetimes, the variable declared in the nested statement block hides the variable declared in the outer statement block l When a local variable is hidden, it is out of scope l l l it cannot be used* The outer variable remains hidden until the lifetime of the inner variable terminates * this is not true for global variables, or those declared in a namespace
l Variable l Lifetime two variables can have the same name if they are declared in different statement blocks #include <iostream> using namespace std; int main() { int a = 10; cout << a << endl; if (3 < 4) { int a = 9; cout << a << endl; } return 0; } The declaration of this variable hides the outside variable.
l Example: What is the output of the following code? #include <iostream> using namespace std; int main() { int a = int b = cout << << 25; 17; " a = " << a " b = " << b << endl; { float a int c = cout << << << = 46. 25; 10; " a = " << a " b = " << b " c = " << c << endl; } cout << " a = " << a << " b = " << b << endl; return 0; }
l We will have much more to say about scope when we talk about functions l global variables l
l. . . Next: short guidelines formatting source code.
Code format guidelines l Comments: each file (purpose, author, date) l complicated code l some variable declarations l
Code format guidelines // // // -----------------A silly program to play with scope Author: Terrence Hill Date: Oct. 13, 1964 ----------------- #include <iostream> using namespace std; int main() { for (int i=0; i<5; i++) { int a; if (i%2 == 0) { int a; a = i; } cout << a << endl; } } file comment
Code format guidelines #include <iostream> using namespace std; int main() { int int num = 15; first = 0; second = 1; total = 0; for (int i = 1; i < num; i++) { total = first + second; first = second; second = total; } cout << total << endl; return 0; } Not clear what this does.
Code format guidelines #include <iostream> using namespace std; int main() { // computing 15 Fibonacci numbers int num = 15; int first = 0; int second = 1; int total = 0; for (int i = 1; i < num; i++) { total = first + second; first = second; second = total; } cout << total << endl; return 0; }
l One l statement/declaration per line exceptions: many short assignments a = 2; b = 3; c = 4; d = 5; // etc. . .
l Empty row between declarations and code l exceptions: short compound blocks
// // // -----------------A silly program to play with scope Author: Terrence Hill Date: Oct. 13, 1964 ----------------- #include <iostream> using namespace std; int main() { for (int i=0; i<5; i++) { int a; if (i%2 == 0) { int a; a = i; } cout << a << endl; } } separate declarations from code short blocks (exceptions)
l Separate compound statements from rest l Nested statements should be indented
// // // -----------------A silly program to play with scope Author: Terrence Hill Date: Oct. 13, 1964 ----------------- #include <iostream> using namespace std; int main() { for (int i=0; i<5; i++) { int a; if (i%2 == 0) { int a; a = i; } cout << a << endl; } } separate compound statements indent
- Slides: 41