Dynamic Memory Allocation in C: calloc, malloc, realloc

4 min 19 sec read Basic


Consider this array declaration,

int students[50]

In this array declaration, memory of 50*4 = 200 bytes will be reserved. Now, suppose you need memory only for 30 students or maybe memory for 70 students. If you notice, there is some restriction for allocation of memory. You have to think always how much bytes an array will require.

What if memory will be allocated at runtime?

This is done using standard library functions malloc() and calloc(). These functions are called as Dynamic Memory Allocation functions in C.

C Memory Management Functions

Functions Description
malloc() This function reserves a block of memory of given size and returns a pointer of type void.
calloc() calloc function is normally used for allocating memory to derived data types such as arrays and structures.
free() releases previously allocated memory
realloc() modify the size of previously allocated space

1) malloc()

malloc function is used to allocate block of memory of given size and returns a pointer of type void. Since malloc() returns a void pointer you can typecast it to integer pointer,float pointer according to the need of program. If it fails to locate enough space it returns a NULL pointer.

Syntax :-

(cast_type *)malloc(Size_in_bytes);

Example :-

#include "alloc.h"
main()
{
  int n, *p;
  printf("Enter the number of students");
  scanf("%d", &n);
  
  p = (int *)malloc(n*4);
  if(p == NULL)
  {
    printf("Memory is not allocated due to some problem");
  }
}

As you see, here memory allocation depends on the user input. If user inputs 20 for Enter the number of students then malloc function will allocate 20*4 = 80 bytes of memory. And (int *) is typecast since malloc returns void pointer.

2) calloc()

calloc() is another memory allocation function that is used for allocating memory at runtime. calloc function is normally used for allocating memory to derived data types such as arrays and structures. If it fails to locate enough space it returns a NULL pointer.

Syntax :-

(cast_type *)calloc(blocks , size_of_block);

Example :-

struct employee
{
 char *name;
 int salary;
};
typedef struct employee emp;
emp *e1;
e1 = (emp*)calloc(30,sizeof(emp));

3) realloc()

realloc() changes memory size that is already allocated to a variable.

Example :-

int *x;
x=(int*)malloc(50 * sizeof(int));
x=(int*)realloc(x,100); //allocated a new memory to variable x

Difference between malloc and calloc

Let's have a look at the difference between calloc and malloc functions..

calloc() malloc()
calloc() initializes the allocated memory with 0 value. malloc() initializes the allocated memory with garbage values.
Syntax :

(cast_type *)calloc(blocks , size_of_block);

Syntax :

(cast_type *)malloc(Size_in_bytes);

Number of arguments is 2 Number of argument is 1

Dynamic Memory Structure

Dynamic Memory tasks are performed inside heap. We will learn more about this in Data Structures tutorials. Global variables, static variables and program instructions get their memory in permanent storage area whereas local variables are stored in area called Stack. The memory space between these two region is known as Heap area. This region is used for dynamic memory allocation during execution of the program. The size of heap keep changing. For now , Memory Structure looks like this ,

Memory Area Storage
Stack --> Local Variables
Heap --> Free memory . Dynamic Memory is allocated here.
Permanent Storage Locations --> Global variables
Permanent Storage Locations --> Program Instructions
Permanent Storage Locations --> static variables

 

 

 

Codzify's

1:1 & 1:Many Mentorship Program

Be job-ready. Learn how to crack the interviews with industry ready training program.


Learn More

Data Structures & Algorithms

What is Dynamic Programming and how to use it

This is a quick introduction to Dynamic Programming.

3 ways to check anagrams

Let's discuss the 3 ways to check anagrams and their time complexity.

Bubble Sort Algorithm

Topics covered: 1) Basics of Bubble Sort Algorithm 2) Worst Case, Best Case complexity

Introduction to Trees

What is a Tree and Terminologies like root, sibling, parent node, children, Leaf node, Ancestor & Descendant, Properties of Tree Data Structure, Depth of node, Height of node

Address Calculation in 2d array

Let's discuss about the Address Calculation using row-major order & column-major order.

Javascript

Javascript - Postfix Vs Prefix

Let's discuss the difference between postfix and prefix with a practical example.

Javascript Identifiers, Variables

In this session, we will discuss the concept of identifiers, variables and operators in javascript with a practical example.

Bootstrap

Bootstrap 4 Tutorial: Modal Popups Made Easy | Live Demo

Let's discuss the Modal Popups in Bootstrap.

How to create a dropdown list in navbar bootstrap

In this video, we will discuss How to create a dropdown list in navbar bootstrap with a practical example.

Navigation tabs in Bootstrap

Let's discuss about the navigation tabs in Bootstrap

Bootstrap Buttons

Let's discuss about the button classes in bootstrap.


Recommended tutorials