Program activation records

k_nitin_r 6,739 views 12 slides Aug 01, 2012
Slide 1
Slide 1 of 12
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12

About This Presentation

Function Calls and Program Activation records / Stack Frames


Slide Content

Function Calls and Program Activation Records Nitin Reddy Katkam nitinkatkam.com n4express.com knreddy.com

Program Activation Records Also called Stack Frames Activation records are entries on a call stack They maintain information about functions being called and their return locations They also contain parameters and local variables

Call Stack Contains an activation record for each function that is called Grows backwards (higher memory address to lower memory address) Has a stack pointer pointing to the top of the stack

Call Stack void function2() { … function2(); … } void function1() { … } int main() { … function1(); … } Each function call adds an activation record to the call stack main function1 function2 function2_1 Stack Pointer

Activation Record Parameters Return Address Caller’s Frame/Base Pointer Local Variables Register Values

Function Call int main() { } main

Function Call ( Contd ) void func1( int a) { int x; } int main() { func1(3); } main func1

Function Call ( Contd ) void func1( int a) { int x; } int main() { func1(3); } main func1 int a Program counter of main() Frame ptr of main() int x Register values

Function Call ( Contd ) An activation record is created on the call stack when a function is called (winding). The activation record is removed from the call stack when the function returns (unwinding). “Why can’t you return a pointer to a local variable declared within a function?”

Function Call ( Contd ) Recursive function calls will add an activation record to the stack for each call “What is a stack overflow ?”

Pointers and Activation Records You can use pointers to access the activation record Example: void func ( int a) { printf (“Return address: %x\n”, &a-1); }

Questions? Email: k.nitin.r [at] gmail