Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Call “Stack”

CS 1037a – Topic 7
Memory Organization in a
Running Program
• Memory available to a running program
is divided into three parts:
• The portion that holds the machine-
language version of the program
• The heap: a pool of memory used for
dynamic allocation of objects
• The call stack: used for all parameters,
local variables, statically-allocated data

7-2
Memory Organization in a
Running Program
• Program section is protected so that it
cannot accidentally be overwritten
• A special register in the CPU called the
program counter (PC) holds the
memory address of the next program
instruction to be executed

7-3
Call Stack
• A stack data structure
• Each item on the call stack is known as
a stack frame (or call frame)
• Call stack manages all function calls
and returns
• Top stack frame determines what
variables, objects are currently
accessible
7-4
Call Stack Example 1
After k+*p is evaluated, but
before return is executed

int fn( int k, int *p) {


return addr /*1*/ in main
return val k = k + 1;
k p
5 12 *p = *p + 1;
return k + *p;
a b c }
4 7 ? int main( void ) {
PC int a = 4, b = 6, c;
return val return addr
c = /*1*/ fn( a, &b);
? somewhere in OS
// rest of program…
7-5
Call Stack Example 1
After return is executed, but before
assignment statement finishes executing

int fn( int k, int *p) {

return val k = k + 1;
12 *p = *p + 1;
return k + *p;
a b c PC }
4 7 ? int main( void ) {
int a = 4, b = 6, c;
return val return addr
c = /*1*/ fn( a, &b);
? somewhere in OS
// rest of program…
7-6
Call Stack Example 1
After c=fn(a,&b); is executed

int fn( int k, int *p) {


k = k + 1;
*p = *p + 1;
return k + *p;
a b c PC }
4 7 12 int main( void ) {
int a = 4, b = 6, c;
return val return addr
c = /*1*/ fn( a, &b);
? somewhere in OS
// rest of program…
7-7
Call Stack vs. Heap Memory
• Call stack is stored contiguously in
memory (i.e.: in consecutive memory
locations)
• “Popped” stack frame will be overwritten
the next time a function is called
• Thus, values in a stack frame are
accessible only while the corresponding
function is being executed

7-8
Call Stack vs. Heap Memory
• In contrast, memory allocated from the
heap is persistent: remains allocated for
the entire program run, unless it is
deallocated by, say, calling a destructor

7-9
Call Stack Example 2
#include “Stack.h”
void fn( Stack<int*> *s1, int main (void) {
Stack<int*> *s2, int n) { int t = 3;
int *j, *k; Stack<int*> * x = new Stack<int*>;
j = new int(n); Stack<int*> * y = new Stack<int*>;
s1->push( j ); fn( x, y, t );
s2 = new Stack<int*>; // rest of main …
n++;
k = new int(n);
s2->push( k );
} // end of function fn
7-10
Call Stack Example 2
Before call to fn:
To save space, we’ll omit
return addresses from the
stack frames.

t 3 y

Call stack Heap


7-11
Call Stack Example 2
In fn, before j=new int(n);

j ?
k ?
s2
n 3
s1

t 3 y

Call stack Heap 7-12


Call Stack Example 2
After j=new int(n);
3
j
k ?
s2
n 3
s1

t 3 y

Call stack Heap 7-13


Call Stack Example 2
After s1->push( j );
3
j
k ?
s2
n 3
s1

t 3 y

Call stack Heap 7-14


Call Stack Example 2
After s2=new Stack<int*>;
3
j
k ?
s2
n 3
s1

t 3 y

Call stack Heap 7-15


Call Stack Example 2
After n++;
3
j
k ?
s2
n 4
s1

t 3 y

Call stack Heap 7-16


Call Stack Example 2
After k=new int(n);
3
j
4
k
s2
n 4
s1

t 3 y

Call stack Heap 7-17


Call Stack Example 2
After s2->push( k );
3
j
4
k
s2
n 4
s1

t 3 y

Call stack Heap 7-18


Call Stack Example 2
After returning from fn Allocated but
3 unreachable:
memory leak
4

t 3 y

Call stack Heap 7-19


Things You Can’t Do
• Exercise: Explain what’s wrong with the
statements in the following function that
are in colour:
int * fn ( int ** n ) {
int j = 7, k = 5;
*n = &j;
return &k;
}

7-20

You might also like