Heap and stack

Write down the difference between the heap and stack?

E

Expert

Verified

The Stack is less or more responsible for keeping track of what's executing in our code (or what's been "called"). While the Heap is more or responsible for keeping the track of our objects.

Assume that Stack is a series of boxes stacked one on top of the next. We keep the track of what's going on in our application by stacking the other box on top every time we call a method (termed as a Frame). We can just use what's in the top box on stack. If we are done with the top box (the method is done executing) we throw it away and carry on using the stuff in the preceding box on the top of stack. The Heap is alike except that its aim is to hold information (not keep track of execution most of time) so whatever thing in our Heap can be accessed at any time. With Heap, there are no constraints as to what can be accessed similar in the stack. The Heap is similar to the heap of clean laundry on our bed which we have not taken the time to put away yet-we can grab what we require quickly. The Stack is similar to the stack of shoe boxes in the closet where we have to take off the top one to get the one beneath it.

   Related Questions in Computer Engineering