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

 

 

 

How was the tutorial? Nice. Right?



Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.



What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp


Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python