I’ve read lot of articles on-line prior on posting about this topic. I will try to combine various articles which talk about it and try to explain it in an interesting and comprehensible manner.
I believe everyone who’s starting or has programmed for some amount of time without looking at the theory too much, has heard of the term stack, the term heap is usually less heard.
After reading quite a bit of both I can say that there are words which are often misinterpreted by many programmers. I’ll try to do my best and not to make any assumptions neither to place some words in a false context.
A stack is a memory place/location where variables which a function executes (int, string, char, bool…) are stored. You can think of a box which can add variables inside and also remove them. When a function executes (we are talking here about any function which can be the main() function which is always executed when a program runs and other functions that we have defined in the code and are calling them inside the main() function) all the variables which are inside that function are stored on the stack. We say that “they are pushed onto the stack”. After the function completes, the variables inside the stack are pushed out of the stack, they are removed from it.
Every stack has a fixed location at the beginning, however this can easily change once variables start to kick in and the stack get’s bigger, the memory pointer is displaced to indicate the current extent of the stack. Remember that when new elements are added to the stack they are always placed on top that box called the stack and the old ones do not get pushed out, they are removed once the function is done with it’s execution only then is the stack freed from the variables that the function contained.
The good thing that we take for granted today is that the memory stack is managed automatically, this means that we don’t have to reserve it manually or free it once we do not need the variables anymore.
Variable scope: also all the variables that are stored on the stack are local not global. This means that the variables lifetime is temporary, after the function returns those variables are immediately destroyed as mentioned earlier.
The stack is also called LIFO (last in first out).
One last thing to mention is the stack-overflow event which can occur if the stack’s capacity get’s exceeded. After the event occurs, the programs crashes.
A heap is also a memory place/location where objects are stored. Often people use the term dynamic memory allocation (don’t think now that the stack is then also referred to as a static memory allocation, this is completely false, we will talk about this in another article). The good thing about it is that you can allocate and deallocate objects at any time and how much you want. As you can see by now the heap is used to store bigger data sets and complex data which are objects. The heap is not arranged like the stack, we can say that the objects are thrown around randomly, you can add and remove the object whenever you want there is no top object like there is in a stack.
Variable scope: also all the variables that are stored on the heap are global not local variables. This means that the variables lifetime is not temporary.
This is pretty much it about the heap. Please read the article about the Garbage Collector to get greater knowledge on how and why are objects removed from the heap. (url: https://programmingcroatia.wordpress.com/2015/08/15/garbage-collector/)
To make a conclusion, here is a useful list about the PROS & CONS taken from stackoverflow (url: http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap)
- Stored in computer RAM just like the heap.
- Variables created on the stack will go out of scope and automatically deallocate.
- Much faster to allocate in comparison to variables on the heap.
- Implemented with an actual stack data structure.
- Stores local data, return addresses, used for parameter passing
- Can have a stack overflow when too much of the stack is used. (mostly from infinite (or too much) recursion, very large allocations)
- Data created on the stack can be used without pointers.
- You would use the stack if you know exactly how much data you need to allocate before compile time and it is not too big.
- Usually has a maximum size already determined when your program starts
- Stored in computer RAM just like the stack.
- Variables on the heap must be destroyed manually and never fall out of scope. The data is freed with delete, delete or free
- Slower to allocate in comparison to variables on the stack.
- Used on demand to allocate a block of data for use by the program.
- Can have fragmentation when there are a lot of allocations and deallocations
- In C++ data created on the heap will be pointed to by pointers and allocated with new or malloc
- Can have allocation failures if too big of a buffer is requested to be allocated.
- You would use the heap if you don’t know exactly how much data you will need at runtime or if you need to allocate a lot of data.
- Responsible for memory leaks
GOOD ARTICLES FOR FURTHER LEARNING: