What exactly is the relationship between stack and heap? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What exactly is the relationship between stack and heap?

Objects are stored in heap but i dont get how is the object in the heap related to stack

26th Aug 2019, 3:00 PM
Occultic_one
Occultic_one - avatar
5 Answers
+ 5
Stack is static memory allocation data, compiler knows the exact size of data going to store at fixed ram location during compilation. Variable like int i = 1; are stored in stack. Stack response is fast. Heap is dynamic memory allocation data, heap also stored in ram, but its address is assigned during run time. Objects are stored in heap, eg. obj = new Class1(); Heap response is slower than stack.
26th Aug 2019, 3:46 PM
Calviղ
Calviղ - avatar
+ 2
In c++, for example, the pointer to the object stored in the heap is stored on the stack, as a local variable. E.g int *i = new int; //i is a pointer stored on the stack, as local variable //i points to an integer store on the heap
26th Aug 2019, 3:21 PM
Théophile
Théophile - avatar
+ 2
One of the difference between Stack and Heap is that Stack is thread specific and Heap is application specific.
27th Aug 2019, 9:38 AM
Moosa Kalanaki
Moosa Kalanaki - avatar
0
When you declare some value type, it takes up memory on the stack, for example: int I = 5, i is on the stack, and 5 is too !! But if you created a class, it takes memory from the heap, because the class is a reference type, but the path to find the class located on the stack,in short the name of the class located on the stack, but value(all content) of the class located on the heap. So, value types are located on the stack, and reference types are on the heap. The stack memory is about 1 MB, but the heap can be 1 GB or more
27th Aug 2019, 10:22 PM
Tigran Saharyan
Tigran Saharyan - avatar