Most often we face situations in programming where the data is dynamic in nature. That is, the number of data items keep changing during execution of the program. For example, consider a program for processing the list of customers of a company. The list grows when names are added and shrinks when names are deleted. When list grows we need to allocate more memory space to the list to accommodate additional data items. Such situations can be handled more easily and effectively by using what is called dynamic data structures.
DYNAMIC MEMORY ALLOCATION
C language requires that the number of elements in an array should be specified at compile time. Our initial judgement of size, if it is wrong, may cause failure of the program or wastage of memory space.
Many languages permit a programmer to specify an array’s size at run time. Such languages take the ability to calculate and assign, during execution, the memory space required by the variables in a program. The process of allocating memory at run time is known as dynamic memory allocation. The library functions used for allocating memory are :

Memory Allocation Process
Let us first look at the memory allocation process associated with a C program. Fig. below shows the conceptual view of storage of a C program in memory.

The program instructions and global and static variables are stored in a region known as
permanent storage area and the local variables are stored in another area called stack. The memory space that is located between these two regions is available for dynamic allocation during execution of the program. The free memory region is called the heap. The size of the heap keeps changing when program is executed due to creation and death of variables that are local to functions and blocks. Therefore, it is possible to encounter memory “overflow” during dynamic allocation process. In such situations, the memory allocations functions men- tioned above returns a NULL pointer.
ALLOCATING A BLOCK OF MEMORY
A block of memory may be allocated using the function malloc. The malloc function reserves a block of memory of specified size and returns a pointer of type void. This means that we can assign it to any type of pointer. It takes the following form;
ptr = ( Cast type * ) malloc ( byte size ) ;
ptr is a pointer of type cast type.
The malloc returns a pointer (of cast type) to an area of memory with size byte - size.
Example :
X = ( int * ) malloc ( 100 *sizeof ( int )) ;
On successful execution of this statement, a memory space equivalent to “100 times the size of an int” bytes is reserved and the address of the first byte of the memory allocated is as- signed to the pointer X of type int.
Similarly, the statement
Cptr = ( char * ) malloc (10) ;
Allocates 10 bytes of space for the pointer Cptr of type char

Remember, the malloc allocates a block of adjacent bytes. The allocation can fail if the space
in the heap is not sufficient to satisfy the request. If it foils, it returns a NULL. We should therefore check whether the allocation is successful before using the memory pointer.
Example :
Write a program that uses a table of integers whose size will be specified interactively at run time.
Program -
# include "stdio.h"
# include "conio.h"
# define NULL O
main ( )
{
int * P, * table ;
int size ;
printf ( “\n What is the sizeof table ? “ ) ;
scanf ( “ % d”, &size ) ;
printf ( “\n” ) ;
if (( table = (int * ) malloc (size * sizeof (int)) = = NULL )
{
printf (“No space available \ n”) ;
exit ( 1) ;
}
printf (“\n address of the first byte is % u\n”, table );
printf(“\n Input table values”);
for ( P = table; P <>
scanf (“%d”, *P );
for ( P = table + size - 1; P > = table; P- - )
printf (“%d is stored at address %u\n”, *P, P );
}
Allocating Multiple Blocks of Memory
calloc is another memory allocation function that is normally used for requesting memory space at runtime for storing derived data types such as arrays and structures. While malloc allocates a single block of storage space, calloc allocates multiple blocks of storage, each of the same size, and then allocates all bytes to O. The general form of calloc is :
ptr = (Cast type * ) calloc ( n, elem_size );
The above statement allocates contiguous space for n blocks, each of size elem-size bytes. All bytes are initialized to zero and a pointer to the first byte of the allocated region is returned. If there is not enough space, a NULL pointer is returned.
The following program allocates space for a structure variable.
#include <>
#include <>
struct student
{
char name (25);
float age;
long int num;
} ;
typedef struct student record ;
record * ptr ;
int class_size = 30 ;
ptr = ( record * ) calloc ( class_size, sizeof ( record )) ;
- - - -
- - - -record is of type struct student having three number :
name, age and num.
The calloc allocates memory to hold data for 30 such records. We should check if the requested memory has been allocated successfully before using the ptr.
This may be done as follows:
if ( ptr == NULL )
{
printf ( “Available memory not sufficient”) ;
exit ( 1 ) ; }
DYNAMIC MEMORY ALLOCATION
C language requires that the number of elements in an array should be specified at compile time. Our initial judgement of size, if it is wrong, may cause failure of the program or wastage of memory space.
Many languages permit a programmer to specify an array’s size at run time. Such languages take the ability to calculate and assign, during execution, the memory space required by the variables in a program. The process of allocating memory at run time is known as dynamic memory allocation. The library functions used for allocating memory are :
Memory Allocation Process
Let us first look at the memory allocation process associated with a C program. Fig. below shows the conceptual view of storage of a C program in memory.
The program instructions and global and static variables are stored in a region known as
permanent storage area and the local variables are stored in another area called stack. The memory space that is located between these two regions is available for dynamic allocation during execution of the program. The free memory region is called the heap. The size of the heap keeps changing when program is executed due to creation and death of variables that are local to functions and blocks. Therefore, it is possible to encounter memory “overflow” during dynamic allocation process. In such situations, the memory allocations functions men- tioned above returns a NULL pointer.
ALLOCATING A BLOCK OF MEMORY
A block of memory may be allocated using the function malloc. The malloc function reserves a block of memory of specified size and returns a pointer of type void. This means that we can assign it to any type of pointer. It takes the following form;
ptr = ( Cast type * ) malloc ( byte size ) ;
ptr is a pointer of type cast type.
The malloc returns a pointer (of cast type) to an area of memory with size byte - size.
Example :
X = ( int * ) malloc ( 100 *sizeof ( int )) ;
On successful execution of this statement, a memory space equivalent to “100 times the size of an int” bytes is reserved and the address of the first byte of the memory allocated is as- signed to the pointer X of type int.
Similarly, the statement
Cptr = ( char * ) malloc (10) ;
Allocates 10 bytes of space for the pointer Cptr of type char
Remember, the malloc allocates a block of adjacent bytes. The allocation can fail if the space
in the heap is not sufficient to satisfy the request. If it foils, it returns a NULL. We should therefore check whether the allocation is successful before using the memory pointer.
Example :
Write a program that uses a table of integers whose size will be specified interactively at run time.
Program -
# include "stdio.h"