my net house

WAHEGURU….!

Understand Stack and Heap from the Ideology of C and C/C++

Writing Flow:
1. Introduction to working of Stack and Heap inside Memory .

2. How Stack Works.

3. How Heap Works.

4. Conclusion based on performance and Ease of use. ::

There are various parts which are responsible to make one complete set of memory which is responsible for Execution and life cycle of application  or process. We can also say that this process is unified either you are using ThreadPool or Process Pool. Whole memory is divided into major three parts, Stack, Heap, Code(Text), Global variables and Constant’s Section, As from the name of last two it can be concluded easily that these parts are responsible for storage of Global Variable and keeping the source code, Other two important parts left are Stack and Heap. Let’s talk about Stack in next Section.

 

Stack is container of one and many functions with the static memory allocated to it, All the memory to stack is allocated before the compile time and it remains same throughout the whole life cycle of application, if there comes more requirement of memory than the allocated one it becomes famous “Stack-over-flow. Main() function call sits on the bottom of the stack. It gets executed at the end of the Stack, All the other Function based on calls sit on each other  in the stack. All the local Variables are also stored in the Stack as well.

 

Heap is something different in terms of ideology from the stack, Where stack is all about occurring of function call  in the series with fixed memory size but in the case of Heap it is all Dynamic. When we need requirement of memory in the runtime without not fixed size heap is there for us. There are multiple ways we can use memory allocations in heap in C and C++. Some known methods are Malloc(), Calloc(), realloc() and free(). C++ is superset of C so all the mentioned functions can also be used in C++ as well, with each call of these functions we get Pointer to Memory block, with that Block one can store data. Make sure one this that in C and C++ memory allocation is not Dynamic so one has to Allocate and De-Allocate memory manually.

Apart from C and C++ there are many other languages those make substantial use of Stacks and Heaps, For example in Python most of the stuff is Stored on heap so  you can allocated data to it even on run time but when you use Generators() or Lazy-Iterators() those are framed on stack and with each continuous  call each functions free the space in the Stack, In the case of Go-lang  something very weird happens, Quoting from StackOverFlow-

It’s worth noting that the words “stack” and “heap” do not appear anywhere in the language spec. Your question is worded with “…is declared on the stack,” and “…declared on the heap,” but note that Go declaration syntax says nothing about stack or heap.

That technically makes the answer to all of your questions implementation dependent. In actuality of course, there is a stack (per goroutine!) and a heap and some things go on the stack and some on the heap. In some cases the compiler follows rigid rules (like “new always allocates on the heap”) and in others the compiler does “escape analysis” to decide if an object can live on the stack or if it must be allocated on the heap.

Source: https://stackoverflow.com/questions/10866195/stack-vs-heap-allocation-of-structs-in-go-and-how-they-relate-to-garbage-collec

 

Source for Stack and Heap of C/C++: https://www.youtube.com/watch?v=_8-ht2AKyH4

 

Note:* Overall  Performance and Consistency of the Application is based on smart use of both Stack and Heap. 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: