Topic :Dynamic Memory Allocation in C++: Stack | Heap | Stack Vs Heap

Dynamic Memory

To Become a Good Programmer, Its essential to know how Dynamic Memory really works. In Our C++ Program The Memory is divided into two parts: first is Stack and second one is Heap. So, to know about What's the Dynamic Memory, and how its exactly work, we need to know about stack and heap. in This tutorial we will discuss about Stack and heap and upcoming tutorials, we will Implement it in C++ using some tools.



Stack:

Whenever we Declared a variable inside a function or outside, it will take Memory from the stack. It's mean our all Declared variable will be inside the stack.



A Stack is an Abstract Data Type, commonly used in most Programming Language. Its behaves like a real stack of plates or cards where we operate it in a LIFO context, mean Last-In-First-Out, And we can only insert or extract element from one end of the container. Whenever We need to added an element in stack, it will goes on the top, and the only element we can remove that will be at the top of the stack.





Let's take an real world example to know about the Operations, we can perform on stack. Let's suppose, there are a pile of 10 Books. an we want to add one more book. so how to do that? simply add the book on the TOP of the pile. How about if we want 5th book from the pile? We need to remove first 4 books from the pile to get the 5th book Then we take the 5th book, and replace all the others back into the pile by adding them from the TOP.



In Case of stack, Adding of the Data/Element is permitted only from the TOP. Removing/Deleting data or element is also done from the TOP.


How Stack Work?

In Stack We need two Function to perform Operation, Known as PUSH (For insertion Operation) and POP (For removal Operation). So every time when a function declare a new variable, it is PUSHED onto the stack. Then Every time a function exits, All of the variable PUSHED onto the stack by that function, are freed or we Can say deleted. Once a stack variable is freed, That region of memory become available for other stack variables. so we don't need to care about the memory, once we don't need it any more.




Heap:

Sometimes We don't want to use stack if we need to pass some memory between different function, or need to keep the memory alive for longer that a single function's execution. for this, we can use Heap.


heap is used to allocate the memory dynamically at run time.

Let's take an real life example to understand how heap work. let's suppose we are in library. there are a lot of books around us. now we need a book "Introduction to Heap in C++". we will ask to Librarian, where we can get this book. he'll show you, or may be he'll get it for you himself if he is Good in his job. In just a moment he'll fetch your desired book from 50,000 book. He won't take a time to searching a book because, he already keep the book in good and reachable way. just like an heap. Now the place from where he got the book is empty, he won't replace that place with others books. you have to get back the book to librarian and he will back it to its position.



Stack Vs Heap

StackHeap
Very fast access(Relatively) slower access
Limit on stack size, its Operating system dependentNo limit on memory size.
Don't have to explicitly De-allocate variablesWe must manage memory. we are responsible to free memory once we don't need it more.
Space is managed efficiently by CPU, memory will not become fragmented.No guaranteed efficient use of space, memory may become fragmented over time as blocks of memory are allocated, then freed.
Variables can't be resizedVariables can be resized using some functions.








I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement