What is the difference between malloc and calloc?

There are two differences. 
First, is in the number of arguments. Malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments. 
Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO. 

  • calloc() allocates a memory area, the length will be the product of its parameters. calloc fills the memory with ZERO's and returns a pointer to first byte. If it fails to locate enough space it returns a NULL pointer.


Syntax: ptr_var=(cast_type *)calloc(no_of_blocks , size_of_each_block); 
i.e. ptr_var=(type *)calloc(n,s);



  • malloc() allocates a single block of memory of REQUSTED SIZE and returns a pointer to first byte. If it fails to locate requsted amount of memory it returns a null pointer.


Syntax: ptr_var=(cast_type *)malloc(Size_in_bytes);
The malloc() function take one argument, which is the number of bytes to allocate, while the calloc() function takes two arguments, one being the number of elements, and the other being the number of bytes to allocate for each of those elements. Also, calloc() initializes the allocated space to zeroes, 

>>>>>>>>>>>>>>

calloc() zero-initializes the buffer, while malloc() leaves the memory uninitialized.
EDIT:
Zeroing out the memory may take a little time, so you probably want to use malloc() if that performance is an issue. If initializing the memory is more important, use calloc(). For example,calloc() might save you a call to memset().while malloc() does not.


>>>>>>>>>>>>>>
size_t cnt = get_int32(file);
struct foo *bar = malloc(cnt * sizeof *bar);
vs.
size_t cnt = get_int32(file);
struct foo *bar = calloc(cnt, sizeof *bar);
>>>>>>>>>>>>>

CallocMalloc
Number of arguments:21
Stands for:Blocks of Memory -AllocationBytes of Memory allocation
Return value:void pointer (void *)void pointer (void *)
Syntax:void *calloc(size_t nelements, size_t bytes);void *malloc(size_t size);
Function:allocates a region of memory large enough to hold "n elements" of "size" bytes each. The allocated region is initialized to zero.allocates "size" bytes of memory. If the allocation succeeds, a pointer to the block of memory is returned.

>>>>>>>>>

Syntax and Examples

malloc()
void *malloc(size_t size);
allocates size bytes of memory. If the allocation succeeds, a pointer to the block of memory is returned.Example:
/* Allocate space for an array with ten elements of type int. */
int *ptr = malloc(10 * sizeof (int));
if (ptr == NULL) {
   /* Memory could not be allocated, so print an error and exit. */
   fprintf(stderr, "Couldn't allocate memory\n");
   exit(EXIT_FAILURE);
} 
/* Allocation succeeded. */
calloc()
void *calloc(size_t nelements, size_t bytes);
allocates a region of memory large enough to hold nelements of size bytes each. The allocated region is initialized to zero. In the above example:
/* Allocate space for an array with ten elements of type int. */
int *ptr = calloc(10,sizeof (int));
if (ptr == NULL) {
   /* Memory could not be allocated, so print an error and exit. */
   fprintf(stderr, "Couldn't allocate memory\n");
   exit(EXIT_FAILURE);
} 
/* Allocation succeeded. */

Related Information

  • malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.
  • calloc(m, n) is the same as
p = malloc(m * n); 
if(p) memset(p, 0, m * n);

Comments

Popular posts from this blog

How to Make REST api call in Objective-C.

Launch iPhone simulator without Xcode.

Hacker Rank problem solution