Memory Allocation

For a computer system to execute a program , the object code must first be loaded into its memory which in most cases is automatically done when issuing the execute command .

The storage space needed for the object code and data are decided at compilation time, and allocated to the program when loaded prior to execution.

The program may need some values to be available throughout its execution , but other values are needed for a short period of time to carry out intermediate calculations .

Therefore the necessary space can efficiently be allocated and utilized if a distinction is made between the 2 types of memory (storage) requirements referred to as static and dynamic.

The allocation and de-allocation of storage locations to a program is primarily done by a memory manager.

Static memory allocation

  1. Global variables and global non dynamic arrays.

  2. Explicitly stated to be static ,  such as using the keyword 'static' in C and Java

Dynamic memory allocation

  1. Function call that requires temporary storage to hold the value of the

    1. parameters .

    2. local variables (non static)

    3. Non dynamic arrays.

    4. return value.

  2. Blocking , control is passed to an inner block and execution for the outer block is suspended . The scope of variables declared within the inner block terminate when the block is completed .; therefore there is no need to hold their values in memory .

  3. Explicit request 'new'

The memory manager uses the stack concept which ensures:

  1. Optimal use of memory

  2. Avoids fragmentation

  3. Speeds up allocation

Stack Storage

The below figure(1) shows the situation in which function A is called and memory is allocated , function B is then called by A and the memory area at the top of the stack is then allocated for it. When B is terminated , the area reserved for it is de-allocated by simply adjusting the stack pointer back to its original position . A similar memory approach is used with blocking .

Example#1 :  Static storage for global variables & dynamic storage for variables and function calls

#include<iostream.h>
int a,b,c;
char ans;

int makePos(int val){
     if(val < 0 ) val=-val;
       return (val);
       }
int max (int d,int e){
     d = makePos(d);
    e = makePos(e);
    if (d >e)
        return (d);
    else
        return (e);
}
void main(){
    do{
    cout <<"\n Enter 2 integers";
    cin >> a;
    cin >> b;
    c = max (a,b);
    cout <<"\nThe value" << c <<" is higher";
    cout <<"\n continue y/n";
    cin >>ans;
    }while(ans=='y');
}

When loading the program prior to execution , static storage is allocated for the variables a,b,c and ans .


When executing the program with input data such as -6 for a and 5 for b , max is called and allocated storage space from the stack to holds its value .

The first statement in max calls the function makePos , and storage space is allocated on top of max to hold the value f (since max would suspend to execute until makePos finishes executing ).

The stack pointer is incremented to provide makePos the necessary space.

After makePos and max are completed , the stack pointer returns to the position below the space allocated for max . Making the space used for the function calls available for reuse.

Upon termination the static memory allocated is released and made available for other programs to use.

 Dynamic storage using 'new '

Command 'new' can be called any time during program execution ; therefore , there could not be any order associated with it .

A special area of memory called the heap is used to serve memory requests of this type.

The memory manager locates an area of memory within the heap big enough for the request and allocates it to the program. The program holds the address of that area to use it during its processing and can release it at any time through the delete command.

 Example#2 : Allocating Memory Dynamically

main ( ){

             int *intptr;              /* A static pointer variable called intptr is declared*/

             intptr = new int ;   /*  Requests the operating system for a new area of memory to hold an integer and stores the memory location for the area in the variable intptr .if not included we should assign intptr to another variable address.*/

            *intptr =100;          /* sets the value of that area to 100 using the pointer variable that holds its address.*/

             delete intptr;         /* de-allocate the area */

             intptr = NULL;        /* initialize intptr to NULL to avoid illegal access to the de-allocated memory area . if not included , future reference to the de-allocated location would result in undesirable effects.*/

... }

 

Fig 3.4  Dynamic Memory Allocation for Example# 2 (P37)

 

Snap Shot of memory allocated for Example#1

Fig 3.5 (P38)

 

Arrays dynamically allocation

 

Example: The following code asks the operating system for an area of memory depending on the students as specified by the user :

int   numStudents;

float *ptr;

cin>>numStudents;

ptr = new float [numstudents];

De-allocation dynamic array :

delete [ ] ptr;

 

Limitation in the dynamic array allocation , is that the array size must be determined at the time of declaration and cannot be changed after that.

 

In many applications the number of locations cannot be specified when the program starts , such as banking system that keeps a record of transactions that has been performed . If the system is to run on 24 hours basis , it is not possible to predict the number of transactions. The system would either write each transaction to a file or use a pointer based linked list .