Dynamic Memory Management


Memory allocation means to reserve the block of memory for some variable. Till now we have discussed that when a variable is declared, the memory needed by that variable is reserved. Since this memory is reserved at the time of compilation, so it is called static memory and the process is called static memory allocation.
            The problem is faced if we declare arrays. Suppose a two dimensional array is declared. In case of static declaration, it is necessary to declare the maximum dimension to be used. For example:
                      
  int a [10] [10];

In this case, (10*10=100) elements so 2*100= 200 bytes of memory is allocated to variable a.

At run time, the order of this matrix is read and supposes the order of matrix is 2*2 i.e. only the memory for four elements is needed. i.e 4*2=8 bytes

Thus 200-8= 192 bytes are wasted.

The only solution to this problem is to allocate memory after entering the order of matrix. Such type of memory that allocated at run time is called dynamic memory. The process of allocating memory at run time is called dynamic memory allocation. By this, we can allocate memory to the variables during execution time.

C provides four dynamic memory allocation functions that you can employ to allocate or reallocate certain memory space while your program is running. These include,

All these functions are part of stdib .h header file. So it is essential to include it, before using these functions.

Previous                                                                                    Next


Powered by Blogger.