---
title: "Malloc , calloc, free and realloc in C"  
description: "Malloc , calloc, free and realloc in C"  
author: "Abhishek Srivasatava"  
published: 2016-10-29  
updated: 2020-09-20  
canonical: https://www.mindstick.com/interview/23093/malloc-calloc-free-and-realloc-in-c  
category: ".net"  
tags: ["computer science"]  
reading_time: 3 minutes  

---

# Malloc , calloc, free and realloc in C

**Malloc**\

By using malloc we can create one block of memory requested dynamically which can be used as array etc. and return a pointer to 1st byte.

Here is the small program to learn how to use malloc function.

```
int main() {int *num_mal;
int num,i;
printf(“Enter the number of element”);
scanf(“%d”,&n);
printf(“Enter the number of element”);
scanf(“%d”,&n);                   num_mal= (int*)malloc(num*(sizeof(int)));                   for(i=0;i<5;i++)                   {                   num_mal[i]=1;                   }                   for(i=0;i<5;i++)                   {                   printf("%d",num_mal[i]);                   }                   }
```

\

**Calloc:**

By using Calloc we can create multiple block of memory requested dynamically which can be used as array etc. it fills the memory to zero and and return a pointer to 1st byte.

Calloc initialize the memory to zero.

Here is the small program to learn how to use calloc function.

```
int main() {                   int *num_cal;                   int num,i;                   printf(“Enter the number of element”);                   scanf(“%d”,&n);                   num_cal= (int*)calloc(num,(sizeof(int)));                   for(i=0;i<5;i++)                   {                   num_cal[i]=1;                   }                   for(i=0;i<5;i++)                   {                   printf("%d",num_cal[i]);                   }                   }
```

\

**Free Function:**

To release the memory we need to use Free function;

```
Syntax: free(num_mal);
```

**Realloc function:**

When we need to change the size of the memory allotted then we need to realloc function.

**Syntax:**

```
num_cal = (int*)calloc(num_cal,n);
```

## Answers

### Answer by Abhishek Srivasatava

**Malloc**\

By using malloc we can create one block of memory requested dynamically which can be used as array etc. and return a pointer to 1st byte.

Here is the small program to learn how to use malloc function.

```
int main() {int *num_mal;
int num,i;
printf(“Enter the number of element”);
scanf(“%d”,&n);
printf(“Enter the number of element”);
scanf(“%d”,&n);                   num_mal= (int*)malloc(num*(sizeof(int)));                   for(i=0;i<5;i++)                   {                   num_mal[i]=1;                   }                   for(i=0;i<5;i++)                   {                   printf("%d",num_mal[i]);                   }                   }
```

\

**Calloc:**

By using Calloc we can create multiple block of memory requested dynamically which can be used as array etc. it fills the memory to zero and and return a pointer to 1st byte.

Calloc initialize the memory to zero.

Here is the small program to learn how to use calloc function.

```
int main() {                   int *num_cal;                   int num,i;                   printf(“Enter the number of element”);                   scanf(“%d”,&n);                   num_cal= (int*)calloc(num,(sizeof(int)));                   for(i=0;i<5;i++)                   {                   num_cal[i]=1;                   }                   for(i=0;i<5;i++)                   {                   printf("%d",num_cal[i]);                   }                   }
```

\

**Free Function:**

To release the memory we need to use Free function;

```
Syntax: free(num_mal);
```

**Realloc function:**

When we need to change the size of the memory allotted then we need to realloc function.

**Syntax:**

```
num_cal = (int*)calloc(num_cal,n);
```


---

Original Source: https://www.mindstick.com/interview/23093/malloc-calloc-free-and-realloc-in-c

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
