0

we have both malloc() and calloc(),both work as same then why they introduced two differen functions?please

3
Contributors
2
Replies
26
Views
3 Years
Discussion Span
Last Post by naveen1993
0

They do not both work the same. calloc() initialized the memory block with all 0's while malloc() does not. And they do not have the same parameters.

-1

With reference to ur query,
malloc() is a pre-defined function which is available in <alloc.h>. We can also create memory without this function. But, whatever the program without this header file will occupies static memory allocation.For example, if you want to create the memory in dynamic memory allocation then go for malloc(),calloc(),realloc() functions. It is used to create the memory dynamically. It required one argument of type, type_size i.e., datatypesize.
It will create the memory in blocks format.
voidmalloc(type_size);
Ex:-
intarr;
arr=(int)malloc(sizeof(int)15);//15 is to create number of elements.

calloc() is a pre-defined function which is available in <alloc.h>. It is used to create the memory dynamically. It required two arguments of type, type_size i.e., datatypesize and count (Number of elements to create).It will create the memory in bytes format.
Syntax:-
voidcalloc(count,type_size);
Ex:-
intarr;
arr=(int)calloc(15sizeof(int));

NOTE:-
It is a permanent memory until u deallocated the memory with the help of free() function.
U can also extend memory by using realloc() function.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.