Lecture 10 Lifetime and Scope of Variables - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Lecture 10 Lifetime and Scope of Variables

Description:

The lifetime of a variable. is the time during program. execution when the variable ... Lifetime and Scope of Static Variables #include iostream int sum(int) ... – PowerPoint PPT presentation

Number of Views:91
Avg rating:3.0/5.0
Slides: 35
Provided by: nikolan
Category:

less

Transcript and Presenter's Notes

Title: Lecture 10 Lifetime and Scope of Variables


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

2
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
a 10d 40
Program Stack
3
After 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
4
Before 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
5
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
play(21.0)
b 21.0
main()
a 20.0b 30
a 10
Program Stack
6
After 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
7
Before 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
8
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
play(0.1)
b 0.1
print(10,30)
a 10b 30
main()
a 20.0b 30
a 10
Program Stack
9
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
10
After 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
11
Lifetime 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
12
Lifetime 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
13
Lifetime 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
14
Lifetime 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
15
Lifetime 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
16
Lifetime 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
17
Lifetime 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
18
Lifetime 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
19
Lifetime 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
20
Lifetime 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
21
Lifetime 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
22
Lifetime 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
23
Lifetime 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
24
Lifetime 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
25
Lifetime 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
26
Lifetime 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
27
Lifetime 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
28
Lifetime 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
29
Lifetime 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
30
Lifetime 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
31
Lifetime 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
32
Lifetime 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
33
Lifetime 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
34
Conclusions
  • 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.
Write a Comment
User Comments (0)
About PowerShow.com