1 / 28

Stack Frames and Functions

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

nida
Download Presentation

Stack Frames and Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stack Frames andFunctions Game102 – Game Development

  2. Stack Operations PUSH – Add an item to TOPPOP – Remove item from TOPConsider a stack of books

  3. 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.

  4. Sample function int big ( int left, int right) { int largest; if (left > right) largest = left; else largest = right; return largest; }

  5. Stack Frame for“big” big int left int right int largest

  6. 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; }

  7. 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

  8. Stack Organization

  9. Another Function int factorial( int n ) { intretval; if ( n <= 1) retval = 1; else retval = n * factorial( n - 1 ); return ( retval ); }

  10. Factorial Flowchart

  11. 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”

  12. Main Program int main() { cout << factorial(6) << endl; return 0; }

  13. The Stack (Maximum) factorial(1) factorial(2) factorial(3) factorial(4) factorial(5) factorial(6) main( )

  14. 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)

  15. 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; }

  16. The main( ) Function int main() { cout << fib(4) << endl; return 0; }

  17. The Stack (Maximum) fib(0) fib(1) fib(2) fib(1) fib(0) fib(1) fib(2) fib(3) fib(4) main( )

  18. 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

  19. 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; }

  20. Advantages of Iteration • ONE stack frame call • VERY fast • Uses explicit loop so it should be easier to follow

  21. Disadvantages of Iteration • More difficult to write code • Longer code

  22. Passing by Value int big ( int left, int right) { int largest; if (left > right) largest = left; else largest = right; return largest; }

  23. 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

  24. 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; }

  25. 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

  26. 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

  27. 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; }

  28. 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”

More Related