Difference between malloc and calloc in C - C-Tutorial

Latest

Saturday, 29 October 2016

Difference between malloc and calloc in C

Difference between malloc and calloc


malloc()
calloc()
malloc allocate the memory at runtime
calloc allocate the memory at runtime
The name malloc stands for memory allocation.
The name calloc stands for contiguous allocation.
malloc() takes one argument that is, number of bytes.
calloc() take two arguments those are number of blocks and size of each block.
malloc() initializes the allocated memory with garbage values.
calloc() initializes the allocated memory with 0 value.
Syntax :
int * ptr = (casttype *) malloc(n * sizeof(int));
Syntax:
int * ptr = (casttype *) calloc(n, sizeof(int));
Ex:  int n=5;

                 10bytes
Ex:  int n=5;





    2bytes     2bytes        2bytes          2bytes          2bytes
The malloc() function allocate single block of memory
The calloc() function allocate multiple blocks of memory

No comments:

Post a Comment