-3

My question here is What is stack and heap memory Why we need both of these memories what are the pros and cons of each

Amanjot Singh
  • 29
  • 1
  • 3
  • 6
  • 3
    Try to answer something easier, like "What is Google?", then try "How do I use Google?". After learning how to search, come back and try it here on StackOverflow, and you might notice that this question has probably been ask 400 gazillion times. – leppie Jan 21 '11 at 10:02
  • 2
    Surely that's in the lecture notes – skaffman Jan 21 '11 at 10:02
  • 2
    Duplicates: http://stackoverflow.com/questions/3469852/diffrence-between-stack-memory-and-heap-memory http://stackoverflow.com/questions/2067126/program-stack-and-heap-how-it-works http://stackoverflow.com/questions/3793660/stack-and-heap-space-for-modern-computers http://stackoverflow.com/questions/2559271/stack-heap-understanding-question – leppie Jan 21 '11 at 10:03
  • I pressurized here on the "WHY" thing – Amanjot Singh Jan 21 '11 at 11:53
  • The answer to "why" is that they do different things. To understand that you need to understand *what* they do. Refer to your lecture notes, other linked questions, Wikipedia (probably) and so on. – Stephen C May 15 '12 at 16:27
  • Here's another one - http://stackoverflow.com/questions/102009/when-is-it-best-to-use-the-stack-instead-of-the-heap-and-vice-versa – Stephen C May 15 '12 at 16:29
  • possible duplicate of [What and where are the stack and heap](http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap) – Stephen C May 15 '12 at 16:30

3 Answers3

2

In a nutshell:

The stack - The memory the program uses to actually run the program. This contains local variables, call-back data (for example when you call a function, the stack stores the state and place you were in the code before you entered the new function), and some other little things of that nature. You usually don't control the stack directly, the variables and data are destroyed, created when you move in and out function scopes.

The heap - The "dynamic" memory of the program. Each time you create a new object or variable dynamically, it is stored on the heap. This memory is controlled by the programmer directly, you are supposed to take care of the creation AND deletion of the objects there.

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
1

In C / C++ language memory allocated onto the stack is automatically free when the allocating scope ends, memory on the heap has to be free with some policy ( free(), delete ... or some garbage collector ). Memory allocated on the heap is visible among different function scope. In the stack we can't allocate big chunk of memory so heap is also useful when tou need to allocate big space for data.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • In "C/C++" you don't even have to have a heap (or stack, other than std::stack) to be a conforming implementation. – Flexo Jan 21 '11 at 10:05
0

I am not sure in which context you are asking but i can answer from their use in memory allocation. Both these data structures are required my platforms like .NET for Garbage collection. Remember all value types are stored on stack and all reference type on heap. This help runtime environment to create an object graph and keep track of what all objects are not in use and can be considered for garbage collection.

Kunal
  • 1,913
  • 6
  • 29
  • 45