What is the difference between "calloc" and "malloc"?

Answer Posted / saugat biswas

Malloc:
Malloc allocates memory but does not initialize it.
Example: char *szPtr = ( char* ) malloc( sizeof( char ) *
100 );
Here szPtr is assigned 100 bytes. But the memory is not
initialized. It contains garbage.

Calloc:
Allocates a block of memory for an array of 'n' elements,
each of them 'l' bytes long, and initializes all its bits
to zero.
Example: char *szPtr = ( char* ) calloc( 100, sizeof(
char ));
Here szPtr is assigned 100 bytes & the memory is
initialized to 0.

Is This Answer Correct ?    11 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

what is data encapsulation in C++?

607


What is the basic difference between C and C++?

626


What are the advantages of inheritance in c++?

657


How static variables and local variablesare similar and dissimilar?

565


How can I learn dev c++ programming?

571






Snake Game: This is normal snake game which you can find in most of the mobiles. You can develop it in Java, C/C++, C# or what ever language you know.

2440


What is a multimap c++?

684


What are move semantics?

675


Write a program using merge () function to combine the elements of array x[ ] and y[ ] into array z[ ].

612


What are c++ files?

581


What is virtual function? Explain with an example

591


Is c++ a low level language?

517


When should we use container classes instead of arrays?

586


Define a pointer to a data member of the type pointer to pointer?

578


why is c++ called oops? Explain

583