DATA STRUCTURES



Admission Enquiry Form

What is Dynamic Memory allocation?

The allocation of the memory at runtime is known as dynamic memory allocation. It is different from static memory allocation because memory is allocated at compile time in the case of static memory allocation.

Functions to allocate dynamic memory in C

  1. malloc()
  2. calloc()
  3. realloc()
  4. free()

The header file of all the functions is <stdlib.h>




malloc() function in C

The malloc() function allocates single block of requested memory.

It doesn't initialize memory at execution time, so it has garbage value initially.

Syntax of malloc() function:

ptr=(cast-type*)malloc(byte-size);

Example

ptr = (int*) malloc(10 * sizeof(int));
Here, the size of int is 4 bytes. So, this statement will allocate 40 bytes of memory. And, the pointer ptr holds the address of the first block of the allocated memory.

Use of malloc() function to allocate memory for one integer value

#include<stdio.h>
#include<stdlib.h>
void main()
{
int *ptr;
ptr=(int*)malloc(sizeof(int)); //memory allocation using malloc()
printf("Enter any number ");
scanf("%d",ptr);
printf("Value of *ptr for int is %d",*ptr);
}


Use of malloc() function to allocate memory for array elements

#include<stdio.h>
#include<stdlib.h>
void main()
{
int n,i,*ptr;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int)); //memory allocation using malloc()
if(ptr==NULL)
{
printf("Memory is not allocated");
exit(0);
}
else
{
printf("Memory successfully allocated using malloc.\n");
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
}
printf("Array elements are: \n");
for(i=0;i<n;++i)
{
printf("%d",*ptr);
ptr++;
}
free(ptr); // It will release the memory allocated by malloc
}

Use of malloc() function to allocate memory for structures

#include <stdio.h>
#include <stdlib.h>
struct student
{
int rollno;
float fee;
char name[30];
};
int main()
{
struct student *ptr;
int n;
printf("Enter the number of records: ");
scanf("%d",&n);

// Memory allocation for no of Records in structure
ptr=(struct student*)malloc(n*sizeof(struct student));
for (int i=0;i<n;++i)
{
printf("Enter name ");
scanf("%s",(ptr+i)->name);
printf("Enter rollno ");
scanf("%d",&(ptr+i)->rollno);
printf("Enter fee ");
scanf("%f",&(ptr+i)->fee);
printf("\n");
}
printf("Displaying Information:\n\n");
printf("Name\tRollno\tFee\n");
for (int i=0;i<n;++i)
{
printf("\n%s\t%d\t%f\n",(ptr+i)->name,(ptr+i)->rollno,(ptr+i)->fee);
}
free(ptr);
return 0;
}




calloc() function in C

The calloc() function allocates multiple block of requested memory.

It initially initialize all bytes to zero.

Syntax of calloc() function:

ptr = (cast-type*)calloc(n, element-size);

Example

ptr=(int*)calloc(10,sizeof(int));
In the above example, the calloc() function has two parameters. The first parameter defines the number of blocks and the second parameter defines the size of each block in memory. The size of the blocks and cast_type is int.It can be in int, char, float, etc.
Return - It returns the base address of the first block to the ptr variable.

Use of calloc() function to allocate memory for one integer value

#include<stdio.h>
#include<stdlib.h>
void main()
{
int *ptr;
ptr=(int*) calloc(1,sizeof(int)); //memory allocation using mcalloc()
printf("Enter any number ");
scanf("%d",ptr);
printf("Value of *ptr for int is %d",*ptr);
}


Use of calloc() function to allocate memory for array elements

#include<stdio.h>
#include<stdlib.h>
void main()
{
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)calloc(n,sizeof(int)); //memory allocation using calloc()
if(ptr!=NULL)
{
printf("Memory successfully allocated using calloc.\n");
printf("Enter elements of array: \n");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
}
printf("Sum of array elements is: \n");
for(i=0;i<n;++i)
{
printf("%d+",*ptr);
sum=sum+(*ptr);
ptr++;
} }
else
{
printf("Memory is not allocated");
exit(0);
}
printf("\b=%d",sum);
free(ptr); // It will release the memory allocated by calloc()
}

Use of calloc() function to allocate memory for structures

#include<stdio.h>
#include<stdlib.h>
struct student
{
int rollno;
float fee;
char name[30];
};
int main()
{
struct student *ptr;
int n;
printf("Enter the number of records: ");
scanf("%d",&n);

// Memory allocation for no of Records of students in structure
ptr=(struct student*) calloc(n,sizeof(struct student));
for (int i=0;i<n;++i)
{
printf("Enter name ");
scanf("%s",(ptr+i)->name);
printf("Enter rollno ");
scanf("%d",&(ptr+i)->rollno);
printf("Enter fee ");
scanf("%f",&(ptr+i)->fee);
printf("\n");
}
printf("Displaying Information:\n\n");
printf("Name\tRollno\tFee\n");
for (int i=0;i<n;++i)
{
printf("\n%s\t%d\t%f\n",(ptr+i)->name,(ptr+i)->rollno,(ptr+i)->fee);
}
free(ptr);
return 0;
}


realloc() function in C

If memory is not sufficient for malloc() or calloc(), you can reallocate the memory by realloc() function. In short, it changes the memory size.

Syntax of realloc() function:

ptr = realloc(ptr, new size);

Example

int *ptr;
ptr = malloc(10*sizeof(int));
ptr=realloc(ptr,100*sizeof(int));

In the above syntax,
realloc - used to resize the memory area which is pointed by ptr.
ptr - the name of the pointer variable which needs to be resized.
new size - the new size of the memory area. It can be smaller or bigger than the actual size.

Example of realloc() function to allocate memory

#include<stdio.h>
#include<stdlib.h>
int main()
{
char *ptr;
ptr = NULL;
ptr = realloc(ptr,10);
if(ptr != NULL)
printf("Memory created successfully\n");
return 0;
}

Use of realloc() function to allocate memory with malloc()

#include<stdio.h>
#include<stdlib.h>
void main()
{
int *ptr,i;
//allocating memory for only 1 integer
ptr=malloc(sizeof(int));
ptr[0] = 10;
printf("%d\n",ptr[0]);
//realloc memory size to store 4 integers
ptr = realloc(ptr, 4 * sizeof(int));
ptr[1] = 20;
ptr[2] = 30;
ptr[3] = 40;
//Displaying values after reallocating memory size
for(i=0;i<4;i++)
{
printf("%d\n",ptr[i]);
}
}


free() function in C

The memory occupied by malloc() or calloc() functions must be released by calling free() function.