In this article dynamic memory allocation in c we give the information about dynamic memory allocation is a method with the help of which, in C language, we can allocate memory at the time of program execution (runtime).

Dynamic Memory Allocation in C:

Dynamic memory allocation is a method with the help of which, in C language, we can allocate memory at the time of program execution (runtime).

C is a structured programming language in which there are some fixed rules of programming. One of which is the rule related to Array.

Array is a group of many elements in which memory is allocated to all the elements in contiguous form. In C language, once the size of an array is declared, it cannot be changed later.

For Example –:

Suppose there is an array of 9 elements in our program and if we need only 5 elements in the program, then the memory given to the remaining array elements will be wasted.

Also, if 9 array elements in the program become full and we need 5 more extra elements, then we will not be able to increase the size of the array from 9 to 14.

In such a situation, the concept of Dynamic Memory Allocation came to solve this problem.

In Dynamic Memory Allocation, we can allocate the required size of memory at the time of program execution.

Memory management functions (malloc(), calloc(), realloc() and free()) are used for memory allocation and deallocation (freeing of memory). These functions are defined in <stdlib.h> header files.

Pointer is used to manage and access dynamic memory. In C language, you cannot do dynamic memory allocation without a pointer because pointer is the only variable which will be able to point the address of that dynamic memory and only through this we can access that dynamic memory. Will be able to access the memory.

Now we know about memory management functions (malloc(), calloc(), realloc() and free()) in detail one by one.

malloc() function in C

malloc() function is used to dynamically allocate memory during program execution. Dynamic memory allocated with the help of malloc() function has a garbage value by default.

When the malloc() function cannot allocate the required memory, it returns a NULL pointer.

Syntax -:

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

Example:

int *a, *ptr;

    a=(int *)malloc(sizeof(int));

The size of int is 2 bytes and in the above example we used (int*) malloc(sizeof(int)); By doing this, 2 bytes of dynamic memory has been allocated.

ptr = (int*) malloc(100 * sizeof(int));

The size of int is 2 bytes and in the above example we used (int*) malloc(100 * sizeof(int)); By doing this, 200 bytes of dynamic memory has been allocated.

Here the ptr pointer holds the first address of this 200 byte memory and as we know the pointer points to whatever address it holds.

Note – If there is not as much space in the computer memory as demanded at the time of dynamic memory allocation then it returns NULL pointer.

Example:

#include< stdio.h >

#include< stdlib.h >

void main()

{

    printf(“\n\n Use of malloc function: \n”);

    int *a,*b,*c;

    a=(int *)malloc(sizeof(int));

    b=(int *)malloc(sizeof(int));

    c=(int *)malloc(sizeof(int));

    printf(“Enter first number:”);

    scanf(“%d”,&*a);

    printf(“Enter second number:”);

    scanf(“%d”,&*b);

    *c=*a+*b;

    printf(“Sum of a and b is:%d\n”,*c);

}

Output:

Use of malloc function:

Enter first number: 10

Enter second number: 20

Sum of a and b is: 30

calloc() function in C

With the help of calloc() function, we can dynamically allocate multiple blocks of memory. It is used to allocate memory for complex data structures like arrays and structures.

It allocates memory dynamically like malloc() function but calloc() is used to allocate multiple blocks of memory space and malloc() is used to allocate single block of memory space.

Dynamic memory created with the help of malloc() has by default garbage value whereas dynamic memory created with the help of calloc() is initialized to zero by default.

If there is not enough space in the memory while allocating dynamic memory with the help of calloc() function then it returns Null.

Syntax -:

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

Here n, no. Of represents the elements and element-size tells what will be the size of each element.

For example -:

ptr = (float*) calloc(25, sizeof(float));

This statement will allocate 25 elements in memory and the size of each element will be equal to the size of the float data type.

Example:

#include< stdio.h >

#include< stdlib.h >

void main()

{

    int *temp,*ptr,i,j;

    printf(“\n\n Use of calloc function: \n”);

    ptr=(int *)calloc(5,2);

    for(i=0;i < 5;i++)

    {

        printf(“Enter the number %d:”,i);

        scanf(“%d”,&*ptr+i);

    }

    for( j=0;j < 5;j++)

    {

        temp=ptr + j;

        printf(“%d\t”,*temp);

    }

}

Output:

Use of calloc function:

Enter the number: 50

51

52

53

54

50        51      52      53        54

realloc() function in C:

realloc() is used to change the size of dynamic memory created with the help of malloc() or calloc(). With the help of realloc() function, we can easily change the size of dynamic memory.

If we want to increase or decrease the size of dynamic memory then we use realloc() function.

Syntax of realloc() Function -:

ptr=realloc(ptr, new-size)

Here ptr will be reallocated with the new size.

Example:

#include< stdio.h >

#include< stdlib.h >

void main()

{

    int *ptr,*ptr1;

    printf(“\n\n Use of realloc function: \n”);

    ptr=(int *)malloc(sizeof(2));

    printf(“Enter the long number:”);

    scanf(“%d”,&*ptr);

    printf(“\nThis number you enter:%d\n”,*ptr);

    ptr1=(int *)realloc(ptr,4);

    printf(“Enter the long number:”);

    scanf(“%d”,&*ptr1);

    printf(“\nThis number you enter:%d\n”,*ptr1);

}

Output:

Use of realloc function:

Enter the long number: 10

This number you enter: 10

Enter the long number: 1000

This number you enter: 1000

free() function in C:

The free() function is used to deallocate or release the dynamic memory created with the help of malloc() or calloc() functions.

Through the free() function, we prevent memory loss. If we do not use the free() function to release or deallocate the dynamically allocated memory, then this memory remains reserved whether it is used in the program or not.

Syntax -:

free(ptr);

Here prt is a pointer variable which points to the dynamic memory space. We are releasing the dynamically allocated memory with the help of this pointer variable and free() function.

Example:

#include< stdio.h >

#include< stdlib.h >

void main()

{

    int *ptr;

    printf(“\n\n Use of realloc function: \n”);

    ptr=(int *)malloc(sizeof(2));

    printf(“Enter the long number:”);

    scanf(“%d”,&*ptr);

    printf(“\nThis number you enter: %d\n”,*ptr);

    free(ptr);

    printf(“\n Memory is released.”);

}

Output:

Use of realloc function:

Enter the long number: 500

This number you enter: 500

Memory is released.

Difference between malloc() and calloc() function:

 Sr. No. malloc() function calloc() function
1 malloc stands for memory allocation calloc stands for contiguous allocation
2 Its allocate a single block of memory of specified size. Its allocate a multiple block of memory of same size.
3 The malloc() function contains only one arguments  that is size of data type.

For example:

int *p;

p=(int *)malloc(sizeof (int));

Here size required 2 bytes

The calloc() function contains two arguments that is number of arguments and size of each memory.

For example:

int *p;

p=(int *)calloc(5, sizeof (int));

Here size required 10 bytes

4 It initializes the allocated memory with garbage value. It initializes the allocated memory with zero.
5 It is faster than calloc() function. It is slower than malloc() function.
6 Syntax:

int *p;

p=(datatype *)malloc(sizeof (data type));

Syntax:

int *p;

p=(datatype *)malloc(n,sizeof (data type));

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *