1

Why we have only two data structure "stack" and "heap"? like int value will be stored in stack and similarly reference type value will be stored in heap.

Why we cannot use some other data structure? Is there any specific reason to use only two these stack and heap.

Thanks a lot.

Thilo
  • 257,207
  • 101
  • 511
  • 656
Aditya Kumar
  • 33
  • 1
  • 8
  • *int value will be stored in stack* is an implementation detail btw, ditto for ref types on the heap. – Alex K. Oct 28 '16 at 12:03
  • 1
    Don't call these "data structures". They are "memory areas". Especially confusing as there are also data structures called "stack" and "heap". – Thilo Oct 28 '16 at 12:03
  • Background reading: http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap – Thilo Oct 28 '16 at 12:08
  • Upvoted, because I don't have a good answer beyond "what more do you need?" (which may actually be the answer, but maybe someone can chime in). – Thilo Oct 28 '16 at 12:09
  • Do you want to count things like code segments, shared memory buffers, memory-mapped I/O devices and memory-mapped files? – Thilo Oct 28 '16 at 12:10
  • 1
    It is rather nonsensical, there are *lots* of wonderful data structures in C#. If you don't use a List or a Dictionary or an array in your program then you are doing it wrong. The terms "heap" and "stack" are just deeply simplified abstractions that have excessively little to do with what happens when you execute C# code. – Hans Passant Oct 28 '16 at 12:24

2 Answers2

0

"The stack", and "the heap" in the context of memory allocation are not data structures. They're memory layout schemes, which can or cannot be implemented with stacks/heaps (respectively).

The existence of these 2 memory layout schemes stems from the need for two different kinds of memory:

  1. Memory that only exists for the duration of a function's lifetime, such as function parameters, local variables, and return values.
  2. Memory that exists for an indefinite duration, which persists even once a function exits.

Let's consider the 2 cases:

Static Memory Allocation

First, imagine if we were dealing with functions that only take parameters and input, and return some value as output, and use no local variables. There needs to be memory allocated that in some way stores the parameters before the function starts, and stores the return value after the function exists. The natural choice of data structure for this is a stack. Every time a new function is called, a new stack frame is allocated on a stack (called the runtime stack), to which the parameters are pushed. Whenever a function exists, its stack frame is popped off the stack, and its return is pushed, so that it can be read by its caller.

This scheme can be extended to support local variables. By definition, any variables that are "local" only exist for the duration of a function's lifetime. Because of this, they fit in very nicely, we can just allocate space for them in a function's stack frame.

This stack can be implemented in any number of ways, as a contiguous array, or a linked list, but ultimately it still acts as a stack, with the ability to only push/pop at the top. That constraint is acceptable because of the qualities of how functions and function calls works: - A function can't exit before all functions it calls have completed. As a consequence, there's no need to ever remove stack frames in positions aren't at the top of the stack. - Only the currently running function can make new function calls. As a consequence, there's no need to ever add stack frames in positions other than the top of the stack.

Dynamic Memory Allocation

Secondly, we have memory that that needs to be able to exist for some indefinite duration that's not bound to the lifetime of the function that defined it. This can be done in a plethora of ways. There could be a contiguous array that's split up and allocated in some way (which is ultimately how RAM works any way), it can be a linked list, or a tree structure. A heap happens to be a type of tree, but this is a coincidence. The term heap, as it pertains to dynamic memory allocation, is meant in the sense of a "bunch" or a "pile", a disorganized collection of things.

The Third Case

Doesn't exist, because there's no third option for memory lifetimes. They're either bound to a function's lifetime, or their not. A third memory allocation scheme would only exist if we had some third requirement that isn't covered by the first two cases.

Alexander
  • 59,041
  • 12
  • 98
  • 151
-2

Stack is a data structure. A heap is not a data structure. A stack can be created in the heap.

In fact, the way you appear to be describing them, they are MEMORY ALLOCATION schemes. Programming languages user a third mechanism: static allocation.

If you go behind the programming languages, a heap is just memory. A stack is just memory. The only thing that makes a stack a stack is that the memory is allocated last in last out. Any memory can be a stack.

It sounds like some elaboration is in order.

Executable and shared library files are actually programs that instruct the loader what to do. When you run a program, the executable file directs the loader to allocate pages of memory. This memory will usually be read only (static data), read/write (initialized program data), read/write demand zero (static data initialized to zero, and read/execute pages. One of blocks the executable directs to be creates is designated as the stack. The EXE instructs the loader to place the Stack Pointer register at (near) the top of this block.

Note that there is no heap when the program starts.

Once the program starts, memory managers are likely to execute. Usually there is only one in a program but it is possible to have multiple memory managers. These memory managers allocate read/write pages of memory that become heaps.

It is entirely possible for the application to use a memory manager to allocate a block of memory and then make that the program stack (rare, but it is common to allocate application stacks for various algorithms).

A stack is a generic data structure. The program stack is a stack that is managed by the stack pointer register. The stack itself is just a block of read/write memory.

A heap is just a block or read/write memory that is controlled by a memory manager. The memory manager will certainly impose a data structure on the heap but that structure is not visible to the application. All the application sees is that pages have been added to its address space.

There are then two levels of dynamic memory allocation.

  1. Page allocation from the operation system
  2. Block allocation from memory managers.

Memory managers have to call #1 to service calls for #2. Generally one refers to memory allocated from memory managers as being "dynamic."

However, the pages loaded in an application can also be dynamic. While the program loader sets up the initial state of the application, the application can modify its page layout by allocating and freeing pages. In most systems, the application can even free pages created by the application loader (but that is likely to cause a crash).

Summing up: 1. The application can have read only, read/write (initialized or demand zero), and read/execute pages of memory. 2. Any page can be made the program stack (but it better be read/write). 3. One or more memory managers can allocate read/write pages and use them for heaps.

user3344003
  • 20,574
  • 3
  • 26
  • 62
  • `Stack is a data structure.` true, ` A heap is not a data structure.` [false](https://en.wikipedia.org/wiki/Heap_(data_structure)), `Programming languages user a third mechanism:` false. `static allocation.` is allocation on the stack. – Alexander Oct 28 '16 at 19:42
  • Static allocation is NOT allocation on a stack It is allocation on memory pages loaded a program startup. Unlike a heap, a static allocation may be either read/write or read only. – user3344003 Oct 28 '16 at 21:57
  • Oh interesting, I misspoke. Let me research that before I edit my answer – Alexander Oct 28 '16 at 21:57
  • Why all answers are downvote where answers make sense? – Aditya Kumar Apr 23 '18 at 14:41