290 likes | 476 Views
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
E N D
Stack Frames andFunctions Game102 – Game Development
Stack Operations PUSH – Add an item to TOPPOP – Remove item from TOPConsider 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 function int 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 Does main( ) create a stack frame when called? What is on the stack frame for main( )? How is the stack organized with multiple stack frames
Another Function int factorial( int n ) { intretval; if ( n <= 1) retval = 1; else retval = n * factorial( n - 1 ); return ( retval ); }
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 Series The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... We can solve this using this formula N = 0… result is 0 N = 1… result is 1 N = n… result is f(n-1) + f(n-2)
Fibonacci Function int fib( int n ) { intretval; if (n == 0) retval = 0; else if (n == 1) retval = 1; else retval = fib(n-1) + fib(n-2); return retval; }
The main( ) Function int 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 Solution int fib( int n ) { intretval; inti; intlastval = 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 Value int big ( int left, int right) { int largest; if (left > right) largest = left; else largest = right; return largest; }
Passing by Value A 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 ADDRESS void swap ( int *pleft, int *pright) { inttmp; 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 val1this 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 REFERENCE void swap ( int &left, int &right) { inttmp; 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 ARGUMENTmakes “left” a reference to the variable that was passed (in this case “val1”) • Modifying “left” will modify “val1”