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 data encapsulation in C++?
What is the basic difference between C and C++?
What are the advantages of inheritance in c++?
How static variables and local variablesare similar and dissimilar?
How can I learn dev c++ programming?
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.
What is a multimap c++?
What are move semantics?
Write a program using merge () function to combine the elements of array x[ ] and y[ ] into array z[ ].
What are c++ files?
What is virtual function? Explain with an example
Is c++ a low level language?
When should we use container classes instead of arrays?
Define a pointer to a data member of the type pointer to pointer?
why is c++ called oops? Explain