0
17kviews
Explain run time storage organization in details.
1 Answer
3
296views
  • An executable program generated by a compiler will have the following organization in memory on a typical architecture (such as on MIPS):

enter image description here

  • This is the layout in memory of an executable program.
  • Note that in a virtual memory architecture (which is the case for any modern operating system), some parts of the memory layout may in fact be located on disk blocks and they are retrieved in memory by demand (lazily).
  • The machine code of the program is typically located at the lowest part of the layout.
  • Then, after the code, there is a section to keep all the fixed size static data in the program.
  • The dynamically allocated data (ie. the data created using malloc in C) as well as the static data without a fixed size (such as arrays of variable size) are created and kept in the heap. The heap grows from low to high addresses.
  • When you call malloc in C to create a dynamically allocated structure, the program tries to find an empty place in the heap with sufficient space to insert the new data; if it can't do that, it puts the data at the end of the heap and increases the heap size.
  • The focus of this section is the stack in the memory layout. It is called the run-time stack.
  • The stack, in contrast to the heap, grows in the opposite direction (upside-down): from high to low addresses, which is a bit counterintuitive. The stack is not only used to push the return address when a function is called, but it is also used for allocating some of the local variables of a function during the function call, as well as for some bookkeeping.

Activate Record

  • It is used to store the current record and the record is been stored in the stack.
  • It contains return value .After the execution the value is been return.
  • It can be called as return value.

Parameter

  • It specifies the number of parameters used in functions.

Local Data

  • The data that is been used inside the function is called as local address

Temporary Data

  • It is used to store the data in temporary variables.

Links

  • It specifies the additional links that are required by the program.

Status

  • It specifies the status of program that is the flag used.
Return value
Parameters
Local Data
Temporary Data
Links(optional)
Status

enter image description here

Storage Allocation

Runtime environment manages runtime memory requirements for the following entities:

  • Code: It is known as the text part of a program that does not change at runtime. Its memory requirements are known at the compile time.
  • Procedures: Their text part is static but they are called in a random manner. That is why, stack storage is used to manage procedure calls and activations.
  • Variables: Variables are known at the runtime only, unless they are global or constant. Heap memory allocation scheme is used for managing allocation and de-allocation of memory for variables in runtime.

Static Allocation

  • In this allocation scheme, the compilation data is bound to a fixed location in the memory and it does not change when the program executes.
  • As the memory requirement and storage locations are known in advance, runtime support package for memory allocation and de-allocation is not required.
  • In a static storage-allocation strategy, it is necessary to be able to decide at compile time exactly where each data object will reside at run time. In order to make such a decision, at least two criteria must be met:

    1. The size of each object must be known at compile time.
    2. Only one occurrence of each object is allowable at a given moment during program execution.
  • Because of the first criterion, variable-length strings are disallowed, since their length cannot be established at compile time. Similarly dynamic arrays are disallowed, since their bounds are not known at compile time and hence the size of the data object is unknown.

  • Because of the second criterion, nested procedures are not possible in a static storage-allocation scheme. This is the case because it is not known at compile time which or how many nested procedures, and hence their local variables, will be active at execution time.

Dynamic Allocation

  • The allocation can be varied during the execution
  • It makes the use of recursive function.
  • In a dynamic storage-allocation strategy, the data area requirements for a program are not known entirely at compilation time.
  • In particular, the two criteria that were given in the previous section as necessary for static storage allocation do not apply for a dynamic storage-allocation scheme.
  • The size and number of each object need not be known at compile time; however, they must be known at run time when a block is entered.
  • Similarly more than one occurrence of a data object is allowed, provided that each new occurrence is initiated at run time when a block is entered.

Stack Allocation

  • Procedure calls and their activations are managed by means of stack memory allocation.
  • It works in last-in-first-out (LIFO) method and this allocation strategy is very useful for recursive procedure calls.

Heap Allocation

  • Variables local to a procedure are allocated and de-allocated only at runtime.
  • Heap allocation is used to dynamically allocate memory to the variables and claim it back when the variables are no more required.
  • Except statically allocated memory area, both stack and heap memory can grow and shrink dynamically and unexpectedly.
  • Therefore, they cannot be provided with a fixed amount of memory in the system.

enter image description here

  • As shown in the image above, the text part of the code is allocated a fixed amount of memory.
  • Stack and heap memory are arranged at the extremes of total memory allocated to the program. Both shrink and grow against each other.
Please log in to add an answer.