+ 8
In the stack, data is automatically allocated/deallocated by your CPU, whereas in the heap you need to manually allocate/deallocate in your code. Allocating memory to stack is faster then heap, but heap is more efficient. Also stack is less flexible, where unlike heap you can't change the size of stored memory.
13th Jun 2019, 6:52 AM
haydenki
haydenki - avatar
+ 15
Kainatic I'll try explaining the differences between stack and heap from the context of the local variables scope within a function call. First, let's recap what many understand about local variable scope within a function before diving into the memory stack and heap. You may have noticed a variable defined within a function is only accessible within the scope of that function. Once the function execution has completed, the variables in that scope are released. These variables can be primitives pointing to actual values and/or reference pointers to objects. This local scope within a function call is stored in memory on a data structure known as the stack. Each function call will allocate new memory for local scope variables on the stack. (continue...)
13th Jun 2019, 1:57 AM
David Carroll
David Carroll - avatar
+ 14
Now... imagine the following functions exist: void A(){ ... } void B(){ A(); } void C(){ B(); A(); } void Main(){ C(); } The first time function A() is called, the stack will have started with Main(), then C() will be added, followed by B(), and finally A(). See below for the Call Stack when A() is called the first time: A() B() C() Main() When A() is finished, it's popped off the stack. Then, B() is popped off leaving the following stack below: C() Main() Then, the next line is called in C(), which pushes A() on the stack with a new local scope: A() C() Main() Then the program finishes once A(), then C(), then Main() are popped off. Again, each item in the stack is the memory for each function. The heap is easy. That's memory used for objects. The heap doesn't automatically deallocate memory as seen with the stack. I like the link below which goes deeper into this for C++: https://www.gribblelab.org/CBootCamp/7_Memory_Stack_vs_Heap.html I hope this made sense.
13th Jun 2019, 2:23 AM
David Carroll
David Carroll - avatar
+ 7
The difference between heap and stack is that stack memory is used to storeĀ local variablesĀ and function call while heap memory is used to store objects in Java. No matter, where the object is created in code e.g. as a member variable, local variable or class variable,Ā Ā they are always created inside heap space in Java. Get complete details from here... https://javarevisited.blogspot.com/2013/01/difference-between-stack-and-heap-java.html?m=1
12th Jun 2019, 10:05 PM
AĶ¢J
AĶ¢J - avatar
+ 5
Good one David. Also, I'd like to add one possibility of having static variables within the scope of functions. By definition, a static variable's lifetime is limited to the lifetime of the "translation unit" and not the scope of the function for which the variable has defined. That means any alteration to a static variable shall be preserved as long as the program runs. Here is an example to illustrate the point. http://cpp.sh/6kkmf
13th Jun 2019, 9:03 AM
Babak
Babak - avatar
+ 4
Thank you for the answers everyone. David Carroll Very informative answer. Thank you. pathetic_millenial Thank you for the short and simple answer.
13th Jun 2019, 9:10 AM
Kainatic [Mostly Inactive Now]
Kainatic [Mostly Inactive Now] - avatar
+ 4
GCs would work on heap and not stack memory.
14th Jun 2019, 3:35 AM
Sonic
Sonic - avatar
+ 3
heap is used to store memory allocated in C and C++, and must be free after use
13th Jun 2019, 12:21 AM
✳AsterisK✳
✳AsterisK✳ - avatar