In this article “Dynamic Memory Allocation in C”, we provide information about how memory can be allocated at the time of program execution (runtime) using special memory management functions in the C language.

Dynamic Memory Allocation in C

Introduction

Dynamic memory allocation (DMA) is a powerful feature in C that allows programmers to allocate and manage memory during runtime instead of compile-time. This means that the program can request exactly the amount of memory it needs while it is running, making it more efficient and flexible.

Why Do We Need Dynamic Memory Allocation?

C is a structured programming language that follows specific rules for handling data structures such as arrays.
When an array is declared in C, its size must be fixed — it cannot be changed later.

Example:
If we declare an array of 9 elements but use only 5, the remaining 4 elements waste memory.
On the other hand, if we need 14 elements but declare only 9, we cannot expand it later.

This limitation leads to inefficient use of memory. To solve this problem, dynamic memory allocation was introduced.
With DMA, the program can request and release memory as needed during execution.

Memory Management Functions in C

C provides four built-in functions for dynamic memory management. These functions are defined in the <stdlib.h> header file:

Function Purpose
malloc() Allocates a single block of memory
calloc() Allocates multiple blocks of memory and initializes them to zero
realloc() Reallocates or resizes previously allocated memory
free() Frees (deallocates) dynamically allocated memory

Dynamic memory allocation in C is always done using pointers, as only pointers can store and access dynamically allocated memory addresses.

  1. malloc() Function in C

The malloc() function is used to allocate a single block of memory during runtime.
The memory allocated by malloc() contains garbage values by default.
If the function fails to allocate memory, it returns a NULL pointer.

Syntax:

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

Example:

int *ptr;

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

Here, malloc() allocates memory for 100 integers. If int is 2 bytes, total memory allocated = 200 bytes.

Example Program:

#include <stdio.h>

#include <stdlib.h>

void main()

{

    printf(“\n\nUse 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

  1. calloc() Function in C

The calloc() function is used to allocate multiple blocks of memory.
Unlike malloc(), it initializes all allocated bytes to zero.

Syntax:

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

Here, n = number of elements and element-size = size of each element.

Example:

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

This allocates memory for 25 floating-point numbers.

Example Program:

#include <stdio.h>

#include <stdlib.h>

void main()

{

    int *ptr, i;

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

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

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

    {

        printf(“Enter number %d: “, i + 1);

        scanf(“%d”, &ptr[i]);

    }

    printf(“\nEntered numbers are:\n”);

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

    {

        printf(“%d\t”, ptr[i]);

    }

}

Output:

Use of calloc() function:

Enter number 1: 10

Enter number 2: 20

Enter number 3: 30

Enter number 4: 40

Enter number 5: 50

Entered numbers are:

10    20    30    40    50

  1. realloc() Function in C

The realloc() function is used to change the size of memory previously allocated with malloc() or calloc().
It helps when we need to increase or decrease the allocated memory at runtime.

Syntax:

ptr = realloc(ptr, new-size);

Example Program:

#include <stdio.h>

#include <stdlib.h>

void main()

{

    int *ptr;

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

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

    printf(“Enter first number: “);

    scanf(“%d”, &ptr[0]);

    printf(“You entered: %d\n”, ptr[0]);

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

    printf(“Enter another number: “);

    scanf(“%d”, &ptr[1]);

    printf(“You entered: %d\n”, ptr[1]);

}

Output:

Use of realloc() function:

Enter first number: 10

You entered: 10

Enter another number: 1000

You entered: 1000

  1. free() Function in C

The free() function is used to release dynamically allocated memory.
It helps prevent memory leaks, which occur when allocated memory is not freed after use.

Syntax:

free(ptr);

Example Program:

#include <stdio.h>

#include <stdlib.h>

void main()

{

    int *ptr;

    printf(“\n\nUse of free() function:\n”);

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

    printf(“Enter a number: “);

    scanf(“%d”, ptr);

    printf(“You entered: %d\n”, *ptr);

    free(ptr);

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

}

Output:

Use of free() function:

Enter a number: 500

You entered: 500

Memory released successfully.

Difference Between malloc() and calloc()

Sr. No. malloc() Function calloc() Function
1 malloc stands for Memory Allocation calloc stands for Contiguous Allocation
2 Allocates a single block of memory Allocates multiple blocks of equal size
3 Takes one argument – size in bytes Takes two arguments – number of elements and size of each element
4 Initializes memory with garbage values Initializes memory with zeros
5 Faster compared to calloc() Slower compared to malloc()
6 Syntax: ptr = (int*) malloc(n * sizeof(int)); Syntax: ptr = (int*) calloc(n, sizeof(int));

Conclusion

Dynamic Memory Allocation in C provides flexibility in managing memory efficiently.
Using functions like malloc(), calloc(), realloc(), and free(), programmers can allocate and release memory as needed during program execution.
This feature is essential for building optimized and memory-efficient programs in C.

See 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 *