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

Should I learn c or c++ first?

568


Why is polymorphism useful?

567


Incase of a function declaration, what is extern means?

521


How many standards of c++ are there?

628


What is the most common mistake on c++ and oo projects?

523






What do you mean by stack unwinding in c++?

732


What is the precedence when there is a global variable and a local variable in the program with the same name?

634


Is c or c++ more useful?

576


Explain the auto storage classes in c++.

601


What doescout<<(0==0) print out a) 0 b) 1 c) Compiler error: Lvalue required

575


Where the memory to the static variables is allocated?

560


What is name hiding in c++?

615


Eplain extern keyword?

565


List the features of oops in c++?

576


What is a storage class? Mention the storage classes in c++.

595