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

Recommended Answers

All 2 Replies

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.