C – Dynamic Memory Allocation5 min read

In this tutorial, you will learn how to dynamically allocate memory in C program using 4 standard library functions: malloc(), calloc(), free() and realloc() with examples. You will also learn the difference between malloc and calloc at the end.

Dynamic Memory Allocation

The main concept of dynamic memory allocation in c programming enables the programmer to allocate memory to the variable at runtime. In C, dynamic memory is allocated from the heap using some standard library functions.

There may be times in a program where you may need to increase the size of an array. So to increase the array size, we can manually allocate the memory during run-time and this allocating and freeing of memory refers to dynamic memory allocation in C programming.

These are mainly four library functions in the stdlib.h header file through which memory allocation is possible in C:

  1. malloc()
  2. calloc()
  3. realloc()
  4. free()
Functionsyntax
malloc()malloc (number *sizeof(int));
calloc()calloc (number, sizeof(int));
realloc()realloc (pointer_name, number * sizeof(int));
free()free (pointer_name);

1. malloc():

This function stands for memory allocation. It is a function that is used to allocate a block of memory dynamically. It takes a single argument that is the amount of memory to allocate in bytes but does not initialize the memory allocated at execution time. So it has a garbage value initially.

Syntax for malloc function:

It is used in the program in the following way:
In the block of code shown below, memory is allocated for space of size 8 bytes to store 8 characters.
Applying it in programming:

Example: C Program for malloc()

Output:

When the program no longer needs the dynamic array, it eventually calls free to return the memory it occupies. The malloc function returns a NULL value if it fails to allocate the specified memory space


2. calloc():

The calloc function stands for contiguous allocation. calloc() function is also like malloc () function. But calloc() initializes the specified bytes to zero. It is mainly used in complex data structures such as arrays and structures.

Calloc() function is used to allocate multiple blocks of memory while malloc() is used for a single block of memory.

Syntax for calloc function:

Example: C Program for calloc():

calloc() needs two arguments that are the number of variables to allocate in memory, and the size is in bytes of a single variable.

Output:


3. realloc():

realloc stands for reallocation of memory. realloc() function is used to change the memory size of the previously allocated memory space or we can say that it increases or decreases the size of the block of memory that is specified to use and also realloc() moves it if necessary.

Syntax for realloc function:

Example: C Program for relloc():

Output:


4. free()

The free() function is called to release/deallocate memory. it releases the specified block of memory and returns it back to the system. It frees the allocated memory of malloc (), calloc (), realloc () function because these functions does not get freed on their own.

Syntax:

C Program for free() with malloc():

Output:


Difference between malloc() and calloc():

malloc()calloc()
Malloc() function will create a single block of memory of size specified by the user.Calloc() function can assign multiple blocks of memory for a variable.
The number of arguments is 2.The number of arguments is 1.
It is not initialized, so contains garbage values.It always initialized to zero
Malloc is faster as compared to calloc.Calloc is slower as compared to malloc.
This function returns only starting address and does not make it zero.This function returns the starting address and makes it zero.
 Time efficiency is higher. Time efficiency is lower.

MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …