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

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More