Title: Lecture 10 Lifetime and Scope of Variables
1Lecture 10Lifetime and Scope of Variables
include ltiostreamgt int a bool
search(float) int main() float c float
b if (search(c1)) int
d float search(float b) int a int c
The lifetime of a variableis the time during
program execution when the variable has storage
assigned to it.
- The lifetime of a global variable is the entire
execution of the program. - The lifetime of a local variable is the
execution of the block in which it is declared .
2include ltiostreamgt int a 10 bool
search(float) int main() float c 20.0
float b 30.0 if (search(c1))
int d 40 bool search(float b) int
a 50 int c 60
Memory
a 10d 40
Program Stack
3After the execution of search() has finished
include ltiostreamgt int a 10 bool
search(float) int main() float c 20.0
float b 30.0 if (search(c1))
int d 40 bool search(float b) int
a 50 int c 60
Memory
main()
c 20.0b 30.0 true
a 10d 40
Program Stack
4Before callingplay() from main()
include ltiostreamgt int a 10 void
play(float)void print(int, int) int main()
float a 20.0 int b 30
play(a1) print(a, b) void play(float
b) void print(int a, int b)
play(0.1)
Memory
main()
a 20.0b 30
a 10
Program Stack
5include ltiostreamgt int a 10 void
play(float)void print(int, int) int main()
float a 20.0 int b 30
play(a1) print(a, b) void play(float
b) void print(int a, int b)
play(0.1)
Memory
play(21.0)
b 21.0
main()
a 20.0b 30
a 10
Program Stack
6After the execution of play() has finishedand
before calling print()
include ltiostreamgt int a 10 void
play(float)void print(int, int) int main()
float a 20.0 int b 30
play(a1) print(a, b) void play(float
b) void print(int a, int b)
play(0.1)
Memory
main()
a 20.0b 30
a 10
Program Stack
7Before callingplay() from print()
include ltiostreamgt int a 10 void
play(float)void print(int, int) int main()
float a 20.0 int b 30
play(a1) print(a, b) void play(float
b) void print(int a, int b)
play(0.1)
Memory
print(10,30)
a 10b 30
main()
a 20.0b 30
a 10
Program Stack
8include ltiostreamgt int a 10 void
play(float)void print(int, int) int main()
float a 20.0 int b 30
play(a1) print(a, b) void play(float
b) void print(int a, int b)
play(0.1)
Memory
play(0.1)
b 0.1
print(10,30)
a 10b 30
main()
a 20.0b 30
a 10
Program Stack
9include ltiostreamgt int a 10 void
play(float)void print(int, int) int main()
float a 20.0 int b 30
play(a1) print(a, b) void play(float
b) void print(int a, int b)
play(0.1)
Memory
print(10,30)
a 10b 30
main()
a 20.0b 30
a 10
Program Stack
10After the execution of print() has finished
include ltiostreamgt int a 10 void
play(float)void print(int, int) int main()
float a 20.0 int b 30
play(a1) print(a, b) void play(float
b) void print(int a, int b)
play(0.1)
Memory
main()
a 20.0b 30
a 10
Program Stack
11Lifetime of Dynamically Allocated Variables
Memory
include ltiostreamgt void freeptr(int p) int
main() int p new int p 10
freeptr(p) p 20 void freeptr(int x)
delete x
Heap
// WRONG!!!!!
main()
p 2395
Program Stack
12Lifetime of Dynamically Allocated Variables
Memory
include ltiostreamgt void freeptr(int p) int
main() int p new int p 10
freeptr(p) p 20 void freeptr(int x)
delete x
Heap
0150
// WRONG!!!!!
2302
main()
p 0150
Program Stack
13Lifetime of Dynamically Allocated Variables
Memory
include ltiostreamgt void freeptr(int p) int
main() int p new int p 10
freeptr(p) p 20 void freeptr(int x)
delete x
Heap
0150
// WRONG!!!!!
10
main()
p 0150
Program Stack
14Lifetime of Dynamically Allocated Variables
Memory
include ltiostreamgt void freeptr(int p) int
main() int p new int p 10
freeptr(p) p 20 void freeptr(int x)
delete x
Heap
0150
// WRONG!!!!!
10
freeptr
x 0150
main()
p 0150
Program Stack
15Lifetime of Dynamically Allocated Variables
Memory
include ltiostreamgt void freeptr(int p) int
main() int p new int p 10
freeptr(p) p 20 void freeptr(int x)
delete x
Heap
// WRONG!!!!!
freeptr
x 0150
main()
p 0150
Program Stack
16Lifetime of Dynamically Allocated Variables
Memory
include ltiostreamgt void freeptr(int p) int
main() int p new int p 10
freeptr(p) p 20 void freeptr(int x)
delete x
Heap
// WRONG!!!!!
CRASH!
main()
p 0150
Program Stack
17Lifetime and Scope of Static Variables
Memory
include ltiostreamgt int sum(int) int main()
int a10, b20, c30 sum(a)
sum(b) sum(c) int sum(int number)
static int s 0 s s number
main()
a 10b 20c 30
s 234
Program Stack
18Lifetime and Scope of Static Variables
Memory
include ltiostreamgt int sum(int) int main()
int a10, b20, c30 sum(a)
sum(b) sum(c) int sum(int num) static
int s 0 s s num
sum(10)
num 10
main()
a 10b 20c 30
s 0
Program Stack
19Lifetime and Scope of Static Variables
Memory
include ltiostreamgt int sum(int) int main()
int a10, b20, c30 sum(a)
sum(b) sum(c) int sum(int num) static
int s 0 s s num
sum(10)
num 10
main()
a 10b 20c 30
s 10
Program Stack
20Lifetime and Scope of Static Variables
Memory
include ltiostreamgt int sum(int) int main()
int a10, b20, c30 sum(a)
sum(b) sum(c) int sum(int num) static
int s 0 s s num
main()
a 10b 20c 30
s 10
Program Stack
21Lifetime and Scope of Static Variables
Memory
include ltiostreamgt int sum(int) int main()
int a10, b20, c30 sum(a)
sum(b) sum(c) int sum(int num) static
int s 0 s s num
sum(20)
num 20
main()
a 10b 20c 30
s 10
Program Stack
22Lifetime and Scope of Static Variables
Memory
include ltiostreamgt int sum(int) int main()
int a10, b20, c30 sum(a)
sum(b) sum(c) int sum(int num) static
int s 0 s s num
sum(20)
num 20
main()
a 10b 20c 30
s 30
Program Stack
23Lifetime and Scope of Static Variables
Memory
include ltiostreamgt int sum(int) int main()
int a10, b20, c30 sum(a)
sum(b) sum(c) int sum(int num) static
int s 0 s s num
main()
a 10b 20c 30
s 30
Program Stack
24Lifetime and Scope of Static Variables
Memory
include ltiostreamgt int sum(int) int main()
int a10, b20, c30 sum(a)
sum(b) sum(c) int sum(int num) static
int s 0 s s num
sum(30)
num 30
main()
a 10b 20c 30
s 30
Program Stack
25Lifetime and Scope of Static Variables
Memory
include ltiostreamgt int sum(int) int main()
int a10, b20, c30 sum(a)
sum(b) sum(c) int sum(int num) static
int s 0 s s num
sum(30)
num 30
main()
a 10b 20c 30
s 60
Program Stack
26Lifetime and Scope of Static Variables
Memory
include ltiostreamgt int sum(int) int main()
int a10, b20, c30 sum(a)
sum(b) sum(c) int sum(int num) static
int s 0 s s num
main()
a 10b 20c 30
s 60
Program Stack
27Lifetime and Scope of Static Variables
Class SharedBank Account Specification
class SharedBankAccount public
SharedBankAccount() // creates an account with
balance 0 and cardno0 SharedBankAccount(int
num) // creates an account with balance 0 and
cardnonum void SetCardNo(int num) int
GetCardNo() const void AddMoney(float money)
// Pre money gt 0 // adds money to balance
void WithdrawMoney(float money) // Pre money
gt 0 // subtracts money from balance if money lt
balance // otherwise does nothingprivate
static float balance // amount of money in
account int cardno // card number
28Lifetime and Scope of Static Variables
Class SharedBank Account Implementation
float SharedBankAccountbalance 0.0
SharedBankAccountSharedBankAccount(int num)
cardno(num) int SharedBankAccountGetCard
No() const return cardno void
SharedBankAccountSetCardNo(int num) cardno
num void SharedBankAccountAddMoney(float
money) balance moneyvoid
SharedBankAccountWithdrawMoney(float money)
if (money lt balance) balance - money
29Lifetime and Scope of Static Variables
A client of class SharedBank Account
int main() SharedBankAccount
card01(4586) card01.AddMoney(120.50)
SharedBankAccount card02(9852)
card02.AddMoney(5.5) card02.WithdrawMoney(50.0)
return 0
main()
SharedBankAccountbalance 0.0
30Lifetime and Scope of Static Variables
A client of class SharedBank Account
int main() SharedBankAccount
card01(4586) card01.AddMoney(120.50)
SharedBankAccount card02(9852)
card02.AddMoney(5.5) card02.WithdrawMoney(50.0)
return 0
main()
SharedBankAccountbalance 120.5
31Lifetime and Scope of Static Variables
A client of class SharedBank Account
int main() SharedBankAccount
card01(4586) card01.AddMoney(120.50)
SharedBankAccount card02(9852)
card02.AddMoney(5.5) card02.WithdrawMoney(50.0)
return 0
main()
SharedBankAccountbalance 120.5
32Lifetime and Scope of Static Variables
A client of class SharedBank Account
int main() SharedBankAccount
card01(4586) card01.AddMoney(120.50)
SharedBankAccount card02(9852)
card02.AddMoney(5.5) card02.WithdrawMoney(50.0)
return 0
main()
SharedBankAccountbalance 126.0
33Lifetime and Scope of Static Variables
A client of class SharedBank Account
int main() SharedBankAccount
card01(4586) card01.AddMoney(120.50)
SharedBankAccount card02(9852)
card02.AddMoney(5.5) card02.WithdrawMoney(50.0)
return 0
main()
SharedBankAccountbalance 76.0
34Conclusions
- The lifetime of a dynamically allocated variable
extends from the time it is allocated (by
operator new) until the time it is deallocated
(by operator delete). - The lifetime of a variable declared with the
static keyword in a standalone function or in a
member function of a class is the entire
execution of the program. - The lifetime of a static data member of a class
is the entire execution of the program. - All identifiers declared within a class are
local to the class (class scope) - The scope of a local identifier (parameter of a
function) includes all statements following the
declaration of the identifier to the end of the
block in which it is declared and includes any
nested blocks unless a local identifier of the
same name is declared in a nested block. - The scope of a global identifier (including
functions) extends from its declaration to the
end of the file in which it is declared,
excluding nested blocks that contain a locally
declared identifier with the same name.