Function Calls and Program Activation records / Stack Frames
Size: 90.45 KB
Language: en
Added: Aug 01, 2012
Slides: 12 pages
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); }