0% found this document useful (0 votes)
51 views18 pages

Stack, Heap &amp Priority Queue Jabeen Gull &amp Zeeshan Aslam Khan

Stack is a place in the computer memory where all the variables that are declared and initialized before runtime are stored. Heap is an area of memory used for dynamic memory allocation. Stack is faster because the access pattern makes it trivial to allocate and deallocate memory from it, while the heap has much more complex bookkeeping involved in an allocation or free.

Uploaded by

Musaab Qamar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views18 pages

Stack, Heap &amp Priority Queue Jabeen Gull &amp Zeeshan Aslam Khan

Stack is a place in the computer memory where all the variables that are declared and initialized before runtime are stored. Heap is an area of memory used for dynamic memory allocation. Stack is faster because the access pattern makes it trivial to allocate and deallocate memory from it, while the heap has much more complex bookkeeping involved in an allocation or free.

Uploaded by

Musaab Qamar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Presentation Topic

Stack ,Heap & Priority Queue

Presenters: Jabeen Gull MC113008 Zeeshan Aslam Khan MC113009

Stack
The stack is a place in the computer memory

Where all the variables


that are declared and initialized before runtime are stored.

Heap
The heap is the section of computer memory where all the variables created or initialized at runtime are stored.

Memory segments

Text (Code) Segment Stack Segment Heap Segment

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

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.

Heap and stack from programming perspective


int 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 */

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 by the runtime, and is reclaimed when the application (technically process) exits

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 more memory from the operating system).

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.

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.

Priority Queue
A PriorityQueue is a data structure that is designed to allow efficient removal of the highest Priority item.

Priority Queue
In 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

Priorities How to schedule and dispatch processes


Priorities quantify the relative importance of processes. Static priorities Dynamic priority

Priority queue
Example :
Doctor

Heart Patient

Fever

Code
Random rand = new Random(); PriorityQueue queue = new PriorityQueue(); // initialize with 1000 elements for(int i=0; i<1000; i++) queue.put(rand.nextDouble()); // exercise queue by turning these over 1000 times for(int i=0; i<10000000; i++) queue.put(queue.get()+rand.nextDouble())

You might also like