Dynamic memory allocation

Dynamic memory allocation

Published by: Nuru

Published date: 21 Jun 2021

Dynamic memory allocation Photo

Dynamic memory allocation

The memory allocation that we have done until now was static memory allocation. The memory that could be used by the program was fixed i.e. we could not increase or decrease the size of memory during the execution of the program. In many applications, it is not possible to predict how much memory would be needed by the program at run time. For example, if we declare an array of integers.
int emp[100];

In an array, it is a must specify the size of the array while declaring, so the size of this array will be fixed during runtime. Now two types of problems may occur.

  1. The number of values to be stored is less than the size of the array then there will be the wastage of memory.
  2. If we want to store more values than the size of the array then we can‟t.

To overcome these problems we should be able to allocate memory at run time. The process of allocating memory at the time of execution is called dynamic memory allocation. The allocation and release of this memory space can be done with the help of some built-in-functions whose prototypes are found in alloc.h and stdlib.h header files.

Pointers play an important role in dynamic memory allocation. It is because we can access the dynamically allocated memory only through pointers.

malloc ( )

This function is used to allocate memory dynamically.

Syntax: pointer_variable=(datatype*) malloc(specified_size);

Here pointer_variable is a pointer of type datatype, and specified_size is the size in bytes required to be reserved in memory.

calloc ( )

The calloc ( ) function is used to allocate multiple blocks of memory. It is somewhat similar to malloc ( ) function except for two differences. The first one is that it takes two arguments. The first argument specifies the number of blocks and the second one specifies the size of each block.

For example: ptr= (int *) calloc (5, sizeof(int));

The other difference between calloc( ) and malloc( ) is that the memory allocated by malloc( ) contains garbage value while the memory allocated by calloc( ) is initialized to zero.

realloc( )

The function realloc( ) is used to change the size of the memory block. It alters the size of the memory block without losing the old data. This is known as the reallocation of memory.

This function takes two arguments, first is a pointer to the block of memory that was previously allocated by malloc( ) or calloc( ) and the second one is the new size for that block.

For example: ptr=(int*) realloc(ptr,newsize);

Program to understand dynamic allocation of memory

#include
#include
#include
void main()
{
int *ptr,n,i;
printf("How many numbers do you want to entered: ");
scanf("%d",&n);
ptr=(int *)malloc(n*sizeof(int));
for(i=0;i {
printf("\n Enter number: ");
scanf("%d",ptr+i);
}
printf("\nDisplaying elements: ");
for(i=0;i {
printf("\n%d",*(ptr+i));
}
getch();
}

Output:

Dynamic memory allocation

The free( ) function

The free( ) function is opposite of malloc( ). Dynamically allocated memory is deallocated with the free function. It returns previously allocated memory to the system.

Syntax: void free(void *p);