7/31/2019 Stack ,Heap & Priority Queue Jabeen Gull & Zeeshan Aslam Khan
1/18
Presentation
Topic
Stack ,Heap & Priority
Queue
7/31/2019 Stack ,Heap & Priority Queue Jabeen Gull & Zeeshan Aslam Khan
2/18
Presenters:
Jabeen GullMC113008
Zeeshan Aslam KhanMC113009
7/31/2019 Stack ,Heap & Priority Queue Jabeen Gull & Zeeshan Aslam Khan
3/18
Stack
The stack is a place in the computer memory
Where all the variables
that are declared and initialized
beforeruntime
are stored.
7/31/2019 Stack ,Heap & Priority Queue Jabeen Gull & Zeeshan Aslam Khan
4/18
Heap
The heap is the section ofcomputer memory
where all the variables
created orinitialized
at runtime
are stored.
7/31/2019 Stack ,Heap & Priority Queue Jabeen Gull & Zeeshan Aslam Khan
5/18
Memory segments
Text (Code) Segment
Stack Segment
Heap Segment
7/31/2019 Stack ,Heap & Priority Queue Jabeen Gull & Zeeshan Aslam Khan
6/18
7/31/2019 Stack ,Heap & Priority Queue Jabeen Gull & Zeeshan Aslam Khan
7/18
What is stack?
The two sections other from the code segment in the
memory are used for data.
1. Automatic variables within functions.
2. Top of the stack
7/31/2019 Stack ,Heap & Priority Queue Jabeen Gull & Zeeshan Aslam Khan
8/18
What is heap?
On the other hand, heap is an area of memory used for
dynamic memory allocation. Blocks of memory are
allocated and freed in this case in an arbitrary order. The
pattern of allocation and size of blocks is not known until
run time. Heap is usually being used by a program
for many different purposes.
7/31/2019 Stack ,Heap & Priority Queue Jabeen Gull & Zeeshan Aslam Khan
9/18
Heap and stack from
programming perspectiveint x; /* static stack storage */
void main() {
int y; /* dynamic stack storage */
char str; /* dynamic stack storage */
str = malloc(50); /* allocates 50 bytes of dynamic
heap storage */
size = calcSize(10); /* dynamic heap storage */
7/31/2019 Stack ,Heap & Priority Queue Jabeen Gull & Zeeshan Aslam Khan
10/18
Scope: The stack is attached to a thread, so when the thread exits
the stack is reclaimed.
The heap is typically allocated at application startup bythe runtime, and is reclaimed when the application
(technically process) exits
7/31/2019 Stack ,Heap & Priority Queue Jabeen Gull & Zeeshan Aslam Khan
11/18
What determines the size of each of them?
The size of the stack is set when a thread is created.
The size of the heap is set on application startup, but can
grow as space is needed (the allocator requests morememory from the operating system).
7/31/2019 Stack ,Heap & Priority Queue Jabeen Gull & Zeeshan Aslam Khan
12/18
What makes one faster?
The stack is faster because the access pattern makes it
trivial to allocate and deallocate memory from it (a
pointer/integer is simply incremented or decremented),
while the heap has much more complex bookkeeping
involved in an allocation or free. Also, each byte in the
stack tends to be reused very frequently which means it
tends to be mapped to the processor's cache, making it
very fast.
7/31/2019 Stack ,Heap & Priority Queue Jabeen Gull & Zeeshan Aslam Khan
13/18
Why is stack and heap important?
When a program is loaded into memory, it takes some
memory management to organize the process. If memory management was not present in your computer
memory, programs would clash with each other leaving
the computer non-functional.
7/31/2019 Stack ,Heap & Priority Queue Jabeen Gull & Zeeshan Aslam Khan
14/18
Priority QueueA PriorityQueue is a data structure
that is designed to allow
efficient removal ofthe highest
Priority
item.
http://c2.com/cgi/wiki?PriorityQueuehttp://c2.com/cgi/wiki?PriorityQueue7/31/2019 Stack ,Heap & Priority Queue Jabeen Gull & Zeeshan Aslam Khan
15/18
Priority QueueIn a simulation event queue, that would typically be the
event which happens next. Your linked list was in fact
such a queue. You removed an item, then added it with a
new priority. My point (if I had one) was that perhaps a
priority queue implementation existed for your use and
you didn't have to create anything. If there wasn't such a
thing, then you did
7/31/2019 Stack ,Heap & Priority Queue Jabeen Gull & Zeeshan Aslam Khan
16/18
PrioritiesHow to schedule and dispatch processes
Priorities quantify the relative importance of processes.
Static priorities
Dynamic priority
7/31/2019 Stack ,Heap & Priority Queue Jabeen Gull & Zeeshan Aslam Khan
17/18
Priority queue Example :
Doctor
FeverHeart Patient
7/31/2019 Stack ,Heap & Priority Queue Jabeen Gull & Zeeshan Aslam Khan
18/18
CodeRandom rand = new Random();
PriorityQueue queue = new PriorityQueue();
// initialize with 1000 elementsfor(int i=0; i