Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Difference Between malloc and calloc
In this post, we will understand the difference between malloc and calloc.
Malloc
The method ‘malloc’ is used to assign a block of memory when it is requested.
It doesn’t clear the memory.
It only initializes the allocated memory when explicitly requested.
It allocates memory of a specific ‘size’.
This size is passed as parameter to it.
This size is allocated from a heap.
It performs its job quickly.
Example
void *malloc(size_t size);
Calloc
It assigns the requested memory to multiple blocks.
This memory allocated is initiated to zero.
This initialization to 0 is done by ‘calloc’ method.
It allocates memory to the required operation of a specific ‘size’, i.e num * size.
The ‘num’ refers to number of blocks of memory.
It is slow in comparison to ‘malloc’ method.
Example
void *calloc(size_t num, size_t size);
