Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable...

Post on 15-Jan-2016

227 views 0 download

Transcript of Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable...

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

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

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.

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.

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.

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

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.

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

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.

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.

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.

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.

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.

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.

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.

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.

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

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

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

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

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.

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

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

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

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?

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?

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

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.

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

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

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

Code format guidelines

Comments:each file (purpose, author, date)

complicated codesome variable declarations

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

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

One statement/declaration per lineexceptions: many short assignments

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

Empty row between declarations and codeexceptions: 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)

Separate compound statements from rest

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