1 / 41

Computer Science 1620

Computer Science 1620. Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs at declaration Variable destruction: memory is returned to the program (for use with another variable) occurs at ….

phuc
Télécharger la présentation

Computer Science 1620

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 1620 Lifetime & Scope

  2. Variable Lifetime • a variable's lifetime is finite • Variable creation: • memory is allocated to the variable • occurs at declaration • Variable destruction: • memory is returned to the program (for use with another variable) • occurs at ….

  3. Variable Destruction • a local variable (the kind we are used to) lives until the end of its statement block • this may be the entire function • this could also be a compound statement • when a variable is destroyed • its memory is no longer reserved – it may be used by another variable • the variable can no longer be used

  4. 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.

  5. 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; } return 0; } Variable num's lifetime End of statement block that num was declared in.

  6. 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; } return 0; } Variable neg's lifetime End of statement block that neg was declared in.

  7. Variable Lifetime – Loops • if a variable is declared inside the loop statement, then its lifetime lasts until the end of that loop statement • 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 • this means that the variable is not redeclared over and over

  8. 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.

  9. Variable Scope • the area of a program where a variable can be used • the scope of a variable is anywhere in the program where: • 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

  10. 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.

  11. 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.

  12. Variable Scope - Example #include <iostream> using namespace std; int main() { if (3 < 4) { int x = 5; } cout << "x = " << x << endl; return 0; } Variable x's lifetime This code generates a compiler error, since we are trying to use x outside of its scope.

  13. Variable Scope - Example #include <iostream> using namespace std; int main() { int x = 0; if (3 < 4) { x = 5; } cout << "x = " << x << endl; return 0; } Variable x's lifetime This code is fine … x is being used within its lifetime.

  14. 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; } return 0; } Variable x's lifetime This code generates a compiler error, since we are trying to use x outside of its scope.

  15. Variable Scope - Example #include <iostream> using namespace std; int main() { for (int x = 0; x < 10; x++) { cout << "x = " << x << endl; } return 0; } Variable x's lifetime This code is fine … we are using x within its lifetime.

  16. 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; return 0; } Variable x's lifetime This code generates a compiler error, since we are trying to use x outside of its scope.

  17. Variable Scope - Example #include <iostream> using namespace std; int main() { for (int x = 0; x < 10; x++, y++) { int y = 2 * x; cout << "y = " << y << endl; } return 0; } Variable y's lifetime This code generates a compiler error, since we are trying to use y outside of its scope.

  18. Nested Code Blocks • we have seen code blocks nested inside other code blocks • 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; }

  19. Nested Code Blocks • we have seen code blocks nested inside other code blocks • 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; }

  20. Nested Code Blocks • we have seen code blocks nested inside other code blocks • standalone compound statements int main() { int x, y; cout << "Please enter two integers: "; cin >> x >> y; { { y++; } x++; } return 0; }

  21. Variable Declaration • until now, we have said that you cannot declare two variables of the same name in the same function • this is actually stronger than the actual rule • you cannot declare two variables of the same name in the same statement block

  22. 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.

  23. Variable 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; }

  24. Variable 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; }

  25. Variable 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; } First variable a's lifetime Second variable a's lifetime Overlap

  26. Variable Lifetime • two variable's with the same name may have overlapping lifetimes • 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?

  27. Variable 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?

  28. Rule: When two variables have overlapping lifetimes, the variable declared in the nested statement block hides the variable declared in the outer statement block • When a local variable is hidden, it is out of scope • 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

  29. Variable 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.

  30. Example: What is the output of the following code? #include <iostream> using namespace std; int main() { int a = 25; int b = 17; cout << " a = " << a << " b = " << b << endl; { float a = 46.25; int c = 10; cout << " a = " << a << " b = " << b << " c = " << c << endl; } cout << " a = " << a << " b = " << b << endl; return 0; }

  31. We will have much more to say about scope when we talk about • functions • global variables

  32. ... Next: short guidelines for formatting source code.

  33. Code format guidelines • Comments: • each file (purpose, author, date) • complicated code • some variable declarations

  34. 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

  35. Code format guidelines #include <iostream> using namespace std; int main() { 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; } Not clear what this does.

  36. 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; }

  37. One statement/declaration per line • exceptions: many short assignments a = 2; b = 3; c = 4; d = 5; // etc...

  38. Empty row between declarations and code • exceptions: short compound blocks

  39. // ---------------------------------- // 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)

  40. Separate compound statements from rest • Nested statements should be indented

  41. // ---------------------------------- // 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

More Related