Computer Science 1620

41
Computer Science 1620 Lifetime & Scope

description

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 …. - PowerPoint PPT Presentation

Transcript of Computer Science 1620

Page 1: Computer Science 1620

Computer Science 1620

Lifetime & Scope

Page 2: Computer Science 1620

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

Page 3: Computer Science 1620

Variable Destructiona 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

Page 4: Computer Science 1620

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.

Page 5: Computer Science 1620

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.

Page 6: Computer Science 1620

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.

Page 7: Computer Science 1620

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

Page 8: Computer Science 1620

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.

Page 9: Computer Science 1620

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

Page 10: Computer Science 1620

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.

Page 11: Computer Science 1620

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.

Page 12: Computer Science 1620

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.

Page 13: Computer Science 1620

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.

Page 14: Computer Science 1620

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.

Page 15: Computer Science 1620

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.

Page 16: Computer Science 1620

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.

Page 17: Computer Science 1620

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.

Page 18: Computer Science 1620

Nested Code Blockswe have seen code blocks nested inside

other code blockse.g. if statementsint 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;}

Page 19: Computer Science 1620

Nested Code Blockswe have seen code blocks nested inside

other code blockse.g. loop statementsint main() {

int x, y; cout << "Please enter two integers: "; cin >> x >> y;

while (x < y) { while (y < 10) { y++; } x++; } return 0;}

Page 20: Computer Science 1620

Nested Code Blockswe have seen code blocks nested inside

other code blocksstandalone compound statementsint main() {

int x, y; cout << "Please enter two integers: "; cin >> x >> y;

{ { y++; } x++; } return 0;}

Page 21: Computer Science 1620

Variable Declarationuntil 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 ruleyou cannot declare two variables of the same

name in the same statement block

Page 22: Computer Science 1620

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.

Page 23: Computer Science 1620

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

Page 24: Computer Science 1620

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

Page 25: Computer Science 1620

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 lifetimeOverlap

Page 26: Computer Science 1620

Variable Lifetime two variable's with the same name may

have overlapping lifetimesone of the variables is declared in a nested

statement blockQuestion: if the variable name is used

inside the overlap area, which variable is being referred to?

Page 27: Computer Science 1620

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?

Page 28: Computer Science 1620

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

Page 29: Computer Science 1620

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.

Page 30: Computer Science 1620

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

Page 31: Computer Science 1620

We will have much more to say about scope when we talk about functionsglobal variables

Page 32: Computer Science 1620

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

Page 33: Computer Science 1620

Code format guidelines

Comments:each file (purpose, author, date)

complicated codesome variable declarations

Page 34: Computer Science 1620

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

Page 35: Computer Science 1620

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.

Page 36: Computer Science 1620

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

Page 37: Computer Science 1620

One statement/declaration per lineexceptions: many short assignments

a = 2; b = 3; c = 4; d = 5; // etc...

Page 38: Computer Science 1620

Empty row between declarations and codeexceptions: short compound blocks

Page 39: Computer Science 1620

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

Page 40: Computer Science 1620

Separate compound statements from rest

Nested statements should be indented

Page 41: Computer Science 1620

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