Stack Frames and Functions

advertisement
Game102 – Game Development
PUSH – Add an item to TOP
POP – Remove item from TOP
Consider a stack of books
•
•
•
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.
int big ( int left, int right)
{
int largest;
}
if (left > right)
largest = left;
else
largest = right;
return largest;
big
•
int left
int right
•
int largest
•
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;
1.
2.
3.
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
int factorial( int n )
{
int retval;
if ( n <= 1)
retval = 1;
else
retval = n * factorial( n - 1 );
return ( retval );
}




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”
int main()
{
cout << factorial(6) << endl;
return 0;
}
factorial(1)
factorial(2)
factorial(3)
factorial(4)
factorial(5)
factorial(6)
main( )
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)
int 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;
int main()
{
cout << fib(4) << endl;
return 0;
}
fib(0)
fib(1)
fib(2)
fib(1)
fib(0)
fib(1)
fib(2)
fib(3)
fib(4)
main( )





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
int 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;



ONE stack frame call
VERY fast
Uses explicit loop so it should be easier to
follow


More difficult to write code
Longer code
int big ( int left, int right)
{
int largest;
if (left > right)
largest = left;
else
largest = right;
return largest;
}
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
void 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;
}


“&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 integer
if you set this pointer to an address, it can
modify values AT that address




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 frame
either GLOBAL VARIABLES or variables on
OTHER stack frames
void 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;
}


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”
Download