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
When should we use container classes instead of arrays?
Explain terminate() and unexpected() function?
Write a program which uses Command Line Arguments
What does the following do: for(;;) ; a) Illegal b) Loops forever c) Ignored by compiler...not illegal
What is the most useful programming language?
How do you establish a has-a relationship?
Define a constructor?
Is java as fast as c++?
What is the main function c++?
Write a program to concatenate two strings.
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?
Who made c++?
Can you pass a vector to a function?
What is a catch statement?
How many ways can a variable be initialized into in C++?