what is calloc and malloc?

Answer Posted / vadivel t

Hi Jhothi16,
how do u say calloc is for reallocation???..

Ans is,

Both are serving for the purpose of dynamic memory
allocation.
But malloc and calloc differs in two ways.

1.After allocating memory using malloc(), the data elements
in the memory will not be initialised. Means, it contains
garbage values.

But calloc() initialise all the data elements to 0.


2.malloc() allocates memory in terms of bytes.it accepts
only one argument, which says no of bytes to be allocated.

But calloc() allocates memory interms of blocks. it is
widely used when there is a need to allocated memory for an
array.

it accepts two arguments, 1st says, no of blocks to be
allocated and next argument says the size of the block.

ex:

calloc(10, sizeof(int))

-> it allocates 40 bytes, if the compiler allocates 4 bytes
for an int variable.

Is This Answer Correct ?    17 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain high-order bytes.

646


Can a function argument have default value?

640


Explain what does the format %10.2 mean when included in a printf statement?

747


application attempts to perform an operation?

1471


What is a const pointer?

604






Can include files be nested?

608


what are bit fields? What is the use of bit fields in a structure declaration?

1464


What Is The Difference Between Null And Void Pointer?

612


What does *p++ do?

558


What are terms in math?

561


Why do we use header files in c?

557


What is the difference between text files and binary files?

649


What is a constant and types of constants in c?

583


What functions are in conio h?

635


What is #line?

582