0% found this document useful (0 votes)
80 views28 pages

J PPL Unit 2 Lecture 3 Life Time

The document discusses the lifetime of variables in programming languages. It covers static, stack-dynamic, explicit heap-dynamic and implicit heap-dynamic variable lifetimes. It also describes the runtime stack, which stores activation records for procedures and their local variables. When a procedure is called, its activation record is pushed onto the stack, and popped off when it returns. The activation record contains space for parameters, local variables, return values and pointers for accessing parent records.

Uploaded by

anon_999010792
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views28 pages

J PPL Unit 2 Lecture 3 Life Time

The document discusses the lifetime of variables in programming languages. It covers static, stack-dynamic, explicit heap-dynamic and implicit heap-dynamic variable lifetimes. It also describes the runtime stack, which stores activation records for procedures and their local variables. When a procedure is called, its activation record is pushed onto the stack, and popped off when it returns. The activation record contains space for parameters, local variables, return values and pointers for accessing parent records.

Uploaded by

anon_999010792
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

PRINCIPLES OF PROGRAMMING

LANGUAGES

Dr. D. C. Kiran
Associate Professor
Department of
Computer Science & Engineering
PRINCIPLES OF PROGRAMMING LANGUAGES

Life Time of Variable

Dr. D. C. Kiran
Department of Computer Science and Engineering
PRINCIPLES OF PROGRAMMING LANGUAGES
Topics To Be Covered

• Memory Layout for Variables

• Run Time Stack

• Activation Record

• Call and Return Sequence


PRINCIPLES OF PROGRAMMING LANGUAGES
Lifetime of Variable

Who is in the memory?


PRINCIPLES OF PROGRAMMING LANGUAGES
Lifetime vs Scope

Lifetime
• Period of time when location is allocated to program
Scope
• Region of program text where declaration is visible
– Inner declaration of x hides outer: “hole in scope”
PRINCIPLES OF PROGRAMMING LANGUAGES
Run Time Environment

• The compiler creates and manages a run-time environment


in which it assumes the target program will be executed
• Includes
• Layout and allocation of storage locations for named
program objects
• Mechanism for the target program to access variables
• Linkages between procedures
• Mechanisms for passing parameters
• Interfaces to the operating system for I/O and other
programs
PRINCIPLES OF PROGRAMMING LANGUAGES
Memory Manager

• The memory manager has one large chunk of memory from


the operating system that it can manage for the application
program
• Allocation – when a program requests memory for a variable
or an object (anything requiring space), the memory
manager gives it the address of a chunk of contiguous heap
memory
• if there is no space big enough, can request the operating
system for virtual space
• if out of space, inform the program
• De-allocation – returns de-allocated space to the pool of
free space
• doesn’t reduce the size of space and return to the
operating system
PRINCIPLES OF PROGRAMMING LANGUAGES
Storage Organization

• Assumes a logical address space


• Operating system will later map it to physical addresses,
decide how to use cache memory, etc.
• Memory typically divided into areas for
• Program code
• Other static data storage, including global constants and
compiler generated data
• Stack to support call/return policy for procedures
• Heap to store data that can outlive a call to a procedure

Code Static Heap Free Memory Stack


PRINCIPLES OF PROGRAMMING LANGUAGES
Life Time of a Variable

The lifetime of a variable is the time during which it is bound


to a particular memory cell
• Static
• Stack-dynamic
• Explicit heap-dynamic
• Implicit heap-dynamic
PRINCIPLES OF PROGRAMMING LANGUAGES
Life Time of a Variable: Static

Bound to memory cells before program execution begins and


remains bound to the same memory cell throughout
execution.
C and C++ static variables

–Advantages:
efficiency – static constants in Java
efficiency - (direct addressing) and no run-time
allocate/deallocate
history-sensitive support

–Disadvantage: lack of flexibility (no recursion)


PRINCIPLES OF PROGRAMMING LANGUAGES
Life Time of a Variable: Stack Dynamic

Storage bindings are created for variables when their


declaration statements are elaborated.
• A declaration is elaborated when the executable code
associated with it is executed.
• Local variables of a method in Java
Advantages
• Allows recursion
• conserves storage

Disadvantages
• Overhead of allocation and de-allocation
• Subprograms cannot be history sensitive
• Inefficient references (indirect addressing)
PRINCIPLES OF PROGRAMMING LANGUAGES
Life Time of a Variable: Explicit Heap-Dynamic

Allocated and De-allocated by explicit directives, specified by


the programmer, which take effect during execution
Referenced only through pointers or references
dynamic objects in C++ (via new and delete)
int *intnode;

intnode = new int;

Delete intnode;
Objects in Java
–Advantage: provides for dynamic storage management
–Disadvantages: inefficient and can be unreliable due to the
complexities of storage management
PRINCIPLES OF PROGRAMMING LANGUAGES
Life Time of a Variable: Implicit Heap-Dynamic

Bound to heap storage only when assigned a value


allocation and de-allocation caused by assignment
statements
all variables in APL; all strings and arrays in Perl,
JavaScript, and PHP
Highs = [74, 80, 85, 90]

•Advantage: flexibility (generic code)


•Disadvantages:
Inefficient, because all attributes are dynamic
Loss of error detection
PRINCIPLES OF PROGRAMMING LANGUAGES
Life Time of a Variable: Example

static int x; Variable Category


int m; 1. x Static
int fun(double a) 2 m Static
{ 3. a Stack
static int z = 0; 4. z Static
int y[100] ; 5. y Stack
int *ptr = new (int); 6. ptr Stack
static char *p= 7 the anonymous object to Heap
{“P",“E",“S",“U"}; which ptr points.
... 8 p Static
} 9 the string PESU which p Heap
is pointing to.
PRINCIPLES OF PROGRAMMING LANGUAGES
Runtime Stack

• Each time a procedure is called (or a block entered),


space for local variables is pushed onto the stack.
• When the procedure is terminated, the space is popped
off the stack.
• If procedure p calls procedure q, then even in cases of
exceptions and errors, q will always terminate before p.
PRINCIPLES OF PROGRAMMING LANGUAGES
Runtime Stack

Activations of procedures during the running of a program can


be represented by an activation tree

Each procedure activation has an activation record (aka


frame) on the run-time stack.
PRINCIPLES OF PROGRAMMING LANGUAGES
Runtime Stack
main() Run Time Stack
{ …; Global Area
g(x);
f(x); Main Main
return(0);
G(X)
F(X)
}
F(X)
void g(int m)
G(x) F(x)
F(X)
{ …
f(y); …

}
F(x)
void f(int n)
{

}
PRINCIPLES OF PROGRAMMING LANGUAGES
Runtime Stack: Finding factorial of 3

fact(1)
1
fact(2) fact(2) fact(2)
2
fact(3) fact(3) fact(3) fact(3) fact(3)
6
main() main() main() main() main() main()

Time 2: Time 3: Time 4: Time 5: Time 6: Time 7:


Push: fact(3) Push: fact(2) Push: fact(1) Pop: fact(1) Pop: fact(2) Pop: fact(3)
returns 1. returns 2. returns 6.

Inside findFactorial(3): Inside findFactorial(2): Inside findFactorial(1):

if (number <= 1) return 1; if (number <= 1) return 1; if (number <= 1) return 1;

else return (3 * factorial else return (2 * factorial else return (1 * factorial (0));
(2)); (1));
PRINCIPLES OF PROGRAMMING LANGUAGES
Run Time Stack: Activation Record

High address
Parameter &
Heap Return value Activation
Record
Control link &
Saved status

Frame pointer (fp)


Stack ptr

Local temps

Stack Parameter &


Return value
Control link &
Static data
Saved status

Code Local temps

Low address
PRINCIPLES OF PROGRAMMING LANGUAGES
Run Time Stack: Activation Record

Temporaries temporaries

Return a value to the calling


Returned value
procedure

Local data Local data

To supply parameters to the called


parameter procedure
Optional access link / To refer to nonlocal data held in the
static link other activation records

Optional control link /


dynamic link Points to the activation record of
the caller
Return Address Info about the state of the machine
just before the proc is called
Format or Layout of the non code part of sub-program
PRINCIPLES OF PROGRAMMING LANGUAGES
Run Time Stack: Activation Record

NIL 1008 sp
return value 1007
c 1006
b 1005
a 1004
ARI of function main

NIL 1003
NIL 1002
NIL 1001
fp return global env 1000
PRINCIPLES OF PROGRAMMING LANGUAGES
Run Time Stack: Call and Return Sequence

• Calling sequence
• Store return address
• Push fp as control link
• Copy sp to fp
• Push arguments
• Reserve space for local variables
• Return sequence
• Copy fp to sp
• Load control link into fp
• Jump to return address
• Change sp
PRINCIPLES OF PROGRAMMING LANGUAGES
Run Time Stack: Call Sequence
PRINCIPLES OF PROGRAMMING LANGUAGES
Run Time Stack: Call Sequence
PRINCIPLES OF PROGRAMMING LANGUAGES
Run Time Stack: Return Sequence
PRINCIPLES OF PROGRAMMING LANGUAGES
Run Time Stack: Return Sequence

NIL 1008 sp
return value 1007
c 1006
b 1005
a 1004
ARI of function main

NIL 1003
NIL 1002
NIL 1001
fp return global env 1000
PRINCIPLES OF PROGRAMMING LANGUAGES
Summary: Take Away

• Difference between Lifetime and Scope

• Memory Layout for Variables


Static, Stack and Heap

• Classification of Variables

• Run Time Stack


-- Activation Record
-- Call and Return Sequence
-- Frame Pointer vs Stack Pointer
THANK YOU

Dr. D. C. Kiran
Department of Computer Science and Engineering
[email protected]
9829935135

You might also like