+ 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
5 Respostas
+ 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.
+ 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
+ 2
One of the difference between Stack and Heap is that Stack is thread specific and Heap is application specific.
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