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
What is the difference between global variables and local variable
Why is c++ called oops?
What are the 3 levels of programming languages?
Where the memory to the static variables is allocated?
When is dynamic checking necessary?
How can a struct in c++ differs from a struct in c?
Who was the creator of c++?
Using a smart pointer can we iterate through a container?
Explain the difference between overloading and overriding?
Explain the properties and principles of oop.
What does iomanip mean in c++?
What are the three forms of cin.get() and what are their differences?
How would you use the functions memcpy(), memset(), memmove()?
What does 7/9*9 equal ? a) 1 b) 0.08642 c) 0
Which programming language's unsatisfactory performance led to the discovery of c++?