Professional Documents
Culture Documents
//
//
root =
root =
root =
root =
root =
root =
root =
root
root
Insert(root,55);
Insert(root,65);
Insert(root,45);
Insert(root,25);
Insert(root,35);
Insert(root,53);
Insert(root,48);
= Insert(root,34);
= Insert(root,38);
HEAP
STACK
STATIC/GLOBAL
CODE/TEXT
Text segment will store all the instructions of the program. The
next segment is used to store all the global variables, all the variables
that are declared outside all the functions. The next one is called stack,
it is a scratch space for function call execution. All the local variables
live in stack. The last one is called Heap. Heap can grow or shrink
dynamically as per requirement. The size of other segments is decided
at compile time whereas Heap can dynamically expand or shrink at run
time, we can control dynamic allocation and de-allocation in heap. We
cannot control dynamic allocation and de-allocation in other segments.
Let us see what happens in stack and heap during program
execution.
When the program will start execution first the main function will
be called, whenever a function is called some amount of memory is
allocated for function execution. The allocated memory is called stack
frame. All the local variables and state of execution of the function call
would be stored in the stack frame of the function call.
MAIN
STACK FRAME
STACK
HEAP
ROOT
0
STACK
FRAME
MAIN
STACK
HEAP
{
root->right = Insert(root->right,data);
}
return root;
}
INSERT
ROOT
DATA
55
ROOT
0
STACK
FRAME
MAIN
STACK
HEAP
GetNewNode
NewNode
200
DATA
55
NULL
INSERT
ROOT
DATA
55
ROOT
55
NULL
STACK
FRAME
MAIN
STACK
HEAP
200
55
NULL
INSERT
ROOT
200
DATA
55
ROOT
200
NULL
STACK
FRAME
MAIN
HEAP
STACK
And now, this particular insert function return the address of the
root and finish. Now main will resume at line root = insert (root,55);
and root is set to 200.
MEMORY MANAGEMENT OF A PROGRAM
NULL
ROOT
55
200
STACK
FRAME
MAIN
STACK
HEAP
NULL
200
Now main control will go to the next line and it will call the insert
function with a value 65, once again execution of main will be paused
and a stack frame will be allocated for execution of insert. Now, this
time for insert call root is not NULL, so program will not enter the first
if condition and it will access the data field of the node at address 200
using a local variable pointer named root.
MEMORY MANAGEMENT OF A PROGRAM
NULL
INSERT
ROOT
200
DATA
65
ROOT
200
55
NULL
200
STACK
FRAME
STACK
FRAME
MAIN
STACK
HEAP
Then the value get compared with the value 65, 65 is greater than
55 so we will go else condition, i.e. line root->right = Insert(root>right,data); now, we are making a recursive call here. Recursion is a
function calling itself. So, Execution of this insert call will be paused
and a new stack frame will be allocated for execution of another insert
call to which the arguments passed are NULL (address of left child) to
local variable root and data i.e. 65.
INSERT
INSERT
ROOT
DATA
65
ROOT
200
DATA
65
ROOT
200
MAIN
STACK
STACK
FRAME
3
NULL
55
NULL
200
STACK
FRAME
2
STACK
FRAME
1
HEAP
So, for this particular insert call root is NULL, so control will go
inside first if condition and we will make a call to GetNewNode
function, now insert will pause and a stack frame will be created to
GetNewNode function, and inside this function there is a local variable
NewNode. In this we are using malloc for memory allocation in heap,
the return address of malloc is passed to NewNode, so let us assume
address be 300.
NewNode
GetNew
Node
INSERT
INSERT
300
DATA
65
ROOT
DATA
65
ROOT
200
DATA
65
ROOT
200
MAIN
STACK
STACK
FRAME
4
STACK
FRAME
3
STACK
FRAME
2
NULL
65
NULL
300
NULL
55
NULL
200
STACK
FRAME
1
HEAP
INSERT
INSERT
ROOT
300
DATA
65
ROOT
200
DATA
65
ROOT
200
MAIN
STACK
STACK
FRAME
3
STACK
FRAME
2
NULL
65
NULL
300
NULL
55
NULL
200
STACK
FRAME
1
HEAP
Now this call to insert will return address 300 and finish. Insert
below will resume at the line (root->right = Insert(root->right,data);) and
now in this insert call right child of this address at node 200 wil be set
as return of the previous insert call which is 300.
INSERT
ROOT
200
DATA
65
ROOT
200
MAIN
STACK
FRAME
2
NULL
65
NULL
300
NULL
55
300
200
STACK
FRAME
1
STACK
HEAP
So now two nodes are linked and finally insert call will finish.
Control will return back to main to the line (root = Insert (root, 65)) root
will be rewritten as 200.
MEMORY MANAGEMENT OF A PROGRAM
ROOT
200
MAIN
STACK
NULL
65
NULL
300
NULL
55
300
200
STACK
FRAME
1
HEAP