stack frames and functions
DESCRIPTION
Stack Frames and Functions. Game102 – Game Development. Stack Operations. PUSH – Add an item to TOP POP – Remove item from TOP Consider a stack of books. Stack frame. Every function call will cause the creation of a stack frame Stack frames contain LOCAL variables and ARGUMENTS to functions - PowerPoint PPT PresentationTRANSCRIPT
STACK FRAMES ANDFUNCTIONS
Game102 – Game Development
STACK OPERATIONSPUSH – Add an item to TOP
POP – Remove item from TOP
Consider a stack of books
STACK FRAME• Every function call will cause the
creation of a stack frame• Stack frames contain LOCAL
variables and ARGUMENTS to functions
• Stack frames are CREATED when functions are called and DESTROYED when functions return.
SAMPLE FUNCTIONint big ( int left, int right){ int largest;
if (left > right) largest = left; else largest = right; return largest;}
Stack Frame for“big”
big
• int left• int right
• int largest
Main Program
int main(){ int val1; int val2; int val3;
cout << "Enter 3 values: " ; cin >> val1 >> val2 >> val3; cout << "Largest value is " << big(val1, big(val2,val3)) << endl; return 0;}
Questions
1. Does main( ) create a stack frame when called?
2. What is on the stack frame for main( )?
3. How is the stack organized with multiple stack frames
Stack Organization
Another Functionint factorial( int n ){ int retval;
if ( n <= 1) retval = 1; else retval = n * factorial( n - 1 ); return ( retval );}
Factorial Flowchart
A Function the Calls Itself Called “recursion” Must have an exit path (typically a
condition that causes it to return a value rather than call itself)
Is dangerous if not used properly Works using the stack and “stack frames”
Main Program
int main(){ cout << factorial(6) << endl; return 0;}
The Stack (Maximum)factorial(1)factorial(2)factorial(3)factorial(4)factorial(5)factorial(6)
main( )
Fibonacci SeriesThe Fibonacci Sequence is the series of numbers:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
We can solve this using this formulaN = 0… result is 0N = 1… result is 1N = n… result is f(n-1) + f(n-2)
Fibonacci Functionint fib( int n ){ int retval;
if (n == 0) retval = 0; else if (n == 1) retval = 1; else retval = fib(n-1) + fib(n-2); return retval;}
The main( ) Functionint main(){ cout << fib(4) << endl; return 0;}
The Stack (Maximum)fib(0)fib(1)fib(2)fib(1)fib(0)fib(1)fib(2)fib(3)fib(4)
main( )
Why the strange display? Every “n” value causes 2 RECURSIVE
paths N = 4 generates recursive N = 3 and N =
2 It is VERY bad when bigger numbers are
used BOTTOM LINE – be very careful if you
used recursion – it is not always fast Fast to write code – not always best
An Iterative Solutionint fib( int n ){ int retval; int i; int lastval = 0; int last2val = 1; int sum;
if (n <= 0) retval = 0; else if (n == 1) retval = 1; else { for (i = 1; i<=n ; i++) { sum = lastval + last2val; last2val = lastval; lastval = sum; } retval = sum; } return retval;}
Advantages of Iteration ONE stack frame call VERY fast Uses explicit loop so it should be easier to
follow
Disadvantages of Iteration More difficult to write code Longer code
Passing by Valueint big ( int left, int right){ int largest;
if (left > right) largest = left; else largest = right; return largest;}
Passing by ValueA COPY of the calling argument gets placed on the stack frame for the function call.
If you call “big( )” with val1 and val2… big(val1,val2)a COPY of val1 and a COPY of val2 are placed into the stack frame of “big ( )” with “left” set to the val1 copy and “right” set to the val2 copy
Passing by ADDRESSvoid swap ( int *pleft, int *pright){ int tmp;
tmp = *pleft; *pleft = *pright; *pright = tmp;}
int main(){ int val1 = 23; int val2 = 12; swap( &val1, &val2); cout << "val1 = " << val1 << " and val2 = " << val2 << endl; return 0;}
Explanation “&val1” means ADDRESS OF val1
this is the memory location of “val1”
“int *pleft” means the VALUE AT pleft is an int“pleft” is consider a POINTER to an integerif you set this pointer to an address, it can modify values AT that address
Explanation On a function call, a stack frame is created When the function returns, the stack frame is
destroyed If you play with argument values and local
variable in a function, they disappear after the function completes
If you want to save information for after the completion of the function, you must modify values OUTSIDE the stack frameeither GLOBAL VARIABLES or variables on OTHER stack frames
Passing by REFERENCEvoid swap ( int &left, int &right){ int tmp;
tmp = left; left = right; right = tmp;}
int main(){ int val1 = 23; int val2 = 12; swap( val1, val2); cout << "val1 = " << val1 << " and val2 = " << val2 << endl; return 0;}
Explanation Specifying “int &left” as an ARGUMENT
makes “left” a reference to the variable that was passed (in this case “val1”)
Modifying “left” will modify “val1”