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

When should we use container classes instead of arrays?

572


Explain terminate() and unexpected() function?

619


Write a program which uses Command Line Arguments

621


What does the following do: for(;;) ; a) Illegal b) Loops forever c) Ignored by compiler...not illegal

685


What is the most useful programming language?

598






How do you establish a has-a relationship?

603


Define a constructor?

581


Is java as fast as c++?

590


What is the main function c++?

577


Write a program to concatenate two strings.

577


If a round rectangle has straight edges and rounded corners, your roundrect class inherits both from rectangle and from circle, and they in turn both inherit from shape, how many shapes are created when you create a roundrect?

655


Who made c++?

603


Can you pass a vector to a function?

531


What is a catch statement?

573


How many ways can a variable be initialized into in C++?

588