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

Can you help me with this one? Make a program that when a user inputed a Product Name, it will display its price, and when the user inputed the quantity of the inputed product, it will show its total price. The output must be like this: Product Name: Price: Quantity: Total Price: ..this is the list of products to be inputed: Cellphone - 1500 Washing Machine - 5200 Television - 6000 Refrigirator - 8000 Oven - 2000 Computer - 11000 thanks..:D

3365


What is a hash function c++?

742


What is the best way to take screenshots of a window with c++ in windows?

773


How many standards of c++ are there?

823


Write bites in Turbo c++ Header ("Include") Files.

908






Write about a nested class and mention its use?

807


Why is c++ awesome?

783


What is the use of register keyword with the variables?

725


Differentiate between the manipulator and setf( ) function?

805


Differentiate between a pointer and a reference with respect to c++.

913


What is rtti in c++?

820


Show the declaration for a static member variable.

703


Is c++ the best programming language?

730


Can static member variables be private?

816


What are the various compound assignment operators in c++?

701