Other contributors have said "Difference between stack memory and heap memory in C?" is the same question as "What is difference between heap memory and stack memory?" If you believe that these are not asking the same thing and should be answered differently, click here

What is difference between heap memory and stack memory?

Answer:

Difference between Stack vs Heap memory

"Stack memory" and "Heap memory" are physically the same.
The same chip of RAM may be used as stack memory when running one program and later used as heap memory when running some other program.
The difference is in how they are used.




Stack


Often a function or method calls another function which in turn calls another function etc.
The execution of all those functions remains suspended until the very last function returns its value.


All the information required to resume the execution of these functions is stored on the stack.


In particular, local variables are stored on the stack.
Local variables are often stored for short amounts of time while a function/method block uses them to compute a task.
Once a function/method has completed its cycle, the space on the stack used by all local variables is freed.


This chain of suspended function calls is the <b>stack</b>, because elements in the stack (function calls) depend on each other.


The stack is important to consider in exception handling and thread executions.


Heap




Heap
The heap is simply the memory used by programs to store global variables.
Element of the heap (variables) have no dependencies with each other and can always be accessed randomly at any time.




All global variables are stored in heap memory.


All variables dynamically created by the program with "new()" or "malloc()" or similar commands are also stored on the heap.


In some programming languages, all instances of an object, including all the attributes of that instance, are stored on the heap.
In those programming languages, local variables of a function that have object type are implemented as creating the new object on the heap,
and storing a reference to that object in the local variable, which is on the stack.
When that function exits, the heap memory used by each local variable that has object is freed, and then all the stack used by that stack is freed.




Comparison
A Heap reference is also stored in Stack memory until the life cycle of the object has completed. Inside the Heap reference all the the contents of the object are stored whereas with a local variable only the variable contents are stored in the stack.
Example:

Stack
var blue
var red
ref 0x456783 (Heap reference)
var tom
ref 0x498702 (Heap reference)
var diane

Heap (0x456783)
name => Susan
age => 26
city => London
height => 5'7
sex => female

Heap (0x498702)
name => Paul
age => 21
city => Glasgow
height => 6'0
sex => male


In addition to heap memory and stack memory, a completely separate section of memory stores the constructors and other methods of a class/object.
Note: There are comments associated with this question. See the discussion page to add to the conversation.
First answer by ID3561813425. Last edit by DavidCary. Contributor trust: 162 [recommend contributor recommended]. Question popularity: 32 [recommend question].