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
![Page 1: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/1.jpg)
STACK FRAMES ANDFUNCTIONS
Game102 – Game Development
![Page 2: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/2.jpg)
STACK OPERATIONSPUSH – Add an item to TOP
POP – Remove item from TOP
Consider a stack of books
![Page 3: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/3.jpg)
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.
![Page 4: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/4.jpg)
SAMPLE FUNCTIONint big ( int left, int right){ int largest;
if (left > right) largest = left; else largest = right; return largest;}
![Page 5: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/5.jpg)
Stack Frame for“big”
big
• int left• int right
• int largest
![Page 6: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/6.jpg)
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;}
![Page 7: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/7.jpg)
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
![Page 8: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/8.jpg)
Stack Organization
![Page 9: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/9.jpg)
Another Functionint factorial( int n ){ int retval;
if ( n <= 1) retval = 1; else retval = n * factorial( n - 1 ); return ( retval );}
![Page 10: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/10.jpg)
Factorial Flowchart
![Page 11: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/11.jpg)
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”
![Page 12: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/12.jpg)
Main Program
int main(){ cout << factorial(6) << endl; return 0;}
![Page 13: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/13.jpg)
The Stack (Maximum)factorial(1)factorial(2)factorial(3)factorial(4)factorial(5)factorial(6)
main( )
![Page 14: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/14.jpg)
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)
![Page 15: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/15.jpg)
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;}
![Page 16: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/16.jpg)
The main( ) Functionint main(){ cout << fib(4) << endl; return 0;}
![Page 17: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/17.jpg)
The Stack (Maximum)fib(0)fib(1)fib(2)fib(1)fib(0)fib(1)fib(2)fib(3)fib(4)
main( )
![Page 18: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/18.jpg)
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
![Page 19: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/19.jpg)
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;}
![Page 20: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/20.jpg)
Advantages of Iteration ONE stack frame call VERY fast Uses explicit loop so it should be easier to
follow
![Page 21: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/21.jpg)
Disadvantages of Iteration More difficult to write code Longer code
![Page 22: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/22.jpg)
Passing by Valueint big ( int left, int right){ int largest;
if (left > right) largest = left; else largest = right; return largest;}
![Page 23: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/23.jpg)
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
![Page 24: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/24.jpg)
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;}
![Page 25: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/25.jpg)
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
![Page 26: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/26.jpg)
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
![Page 27: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/27.jpg)
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;}
![Page 28: Stack Frames and Functions](https://reader036.vdocuments.us/reader036/viewer/2022062315/56815f5a550346895dce3c9e/html5/thumbnails/28.jpg)
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”