Differentiate between Static, Stack, and Heap allocation strategies

Static Allocation:

  • The simpler form of allocation is static allocation.
  • Storage space once allocated was never released.
  • So a very simple storage allocation model that allocated storage, as required, from one end of the available space towards the other, was adequate.
  • Early programming languages, such as FORTRAN, had static storage, the amount of which was known at compile time.
  • Static storage allocation is efficient since no time or space is expended for storage management during execution.
  • The translator can generate a direct value address for all data items.
  • It is incompatible with recursive subprogram calls, with data structures whose size is dependent on computed or input data, and with many other desirable language features.

Stack Based Storage Management:

  • It is the simpler run-time storage management technique.
  • Storage allocation begins at one end.
  • Storage must be freed in the reverse order of allocation so block allocated space recently must free the space first.
  • Only a single stack pointer is all that is needed to control storage management.
  • The stack pointer always points to the top of the stack, the next available word of free storage in the stack block.
  • Compaction occurs automatically as part of freeing storage.
  • Freeing a block of storage automatically recovers the freed storage and makes it available for reuse.
  • We know that most of the time program and data elements requiring storage are tied to subprogram activations.
  • When a subprogram is called, per call a new activation record is created on the top of the stack and termination causes its deletion from the stack.

The Heap Storage Management

  • The heap is used for the storage of values which may require to be accessible from the time the storage is allocated until the program terminates.
  • The major problem that arises in this type of allocation is that how to reallocate or reuse the allocated space.
  • Some of the languages like C follow the following method to reallocated the allocated space: ptr = malloc (6);
  • The above statement allocates six bytes of space and returns a pointer to it.
  • Now, these storage spaces may become inaccessible due to program actions such as reassignment to pointers; etc.
  • ptr = ptrnew;
  • Now ptr contains the address of ptrnew and space allocated by the above malloc (6) statement becomes inaccessible.
  • There remains the possibility that the space concerned is accessible via some other variables. Consider the effect of executing an assignment such as
  • ptr1 = ptr;
  • This assignment would make the space allocated by malloc accessible via the variable ptr1.
  • But we have no method by which we can know which of the path a program will follow at run time. This means the code to reclaim heap space cannot be generated at compile-time, despite the fact that large areas of space allocated on the heap can actually become inaccessible.
  • Language C provides the facility to free the space as follows: free (ptr);
  • But these kinds of facilities put a considerable responsibility on the programmer.
  • Now languages are available in which programmer’s responsibility to free inaccessible storage is automatic, for example, JAVA provides facilities to do so.
  • Incidentally, JAVA stores arrays on the heap, unlike the storage models as above. All objects are also stored on a heap.

Leave a Reply