Click here to Skip to main content
15,922,325 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
what is meant by stack and heap and what is the difference between them
Posted

A stack is an area of memory that is thread specific and from which values are allocated from the top each time you enter a method.
A heap is a different area of memory that is not thread specific and from which values are allocated as needed.

The big difference is that stack based values are temporary. when you leave the method in which they were created, they are gone, and trying to use them again will cause massive problems. Heap values do not have this limitation.

Stack values are faster - allocation is much, much easier because you do not have to look for a space, allocate it, and prepare it for use - you just go "Variable = top of stack, top of stack += size of variable" because it will all be deallocated when the method ends. Heap can't do that, it needs to keep references to what is where so it can be deleted properly and the space re-used later.

It's a complex subject, but Google and MSDN can help you find other information.
 
Share this answer
 
look This[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900