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 check Krishnamurthy Number

In this tutorial, we will learn about Krishnamurthy numbers and write a Krishnamurthy Number program in Java. We will write two programs for Krishnamurthy number …
Read More

Java Program for ISBN Number

In this tutorial, we will learn about the ISBN (International Standard Book Number) and write a program to check for the ISBN Number in Java …
Read More

C++ Memory Management

Memory management refers to the process of managing the computer memory while assigning the space to the program’s variable to improve the overall performance. Requirement …
Read More

C++ Pointer to Pointer

As we know by now that a pointer stores the address of the pointed variable. But it is not the only use, pointer also stores …
Read More

C++ Passing Pointers to Functions

A function is a user-defined block of codes that executes some specific task assigned to it invoked by its name. If there is an argument …
Read More

C++ Null Pointer

We use a null pointer when we do not have the exact address to assign to a pointer. It is considered a good practice and …
Read More