WHAT IS THE DIFFERENCE BETWEEN malloc() and calloc() in c
file management?

Answer Posted / dilip k. singh(dks)

Malloc:
1. Takes only 1 argument- the size of the memory block to
be allocated.
2. Allocates memory as a single contiguous block.
3. Will fail if a single contiguous memory block of
required size is not available.

Calloc:
1. Takes two arguments - the number of memory blocks needed
and the size of each memory block.
2. It may or may not allocate a single contiguous block,
thus will not fail if a single contiguous memory block
of required size is not available.
3. Initialises the memory blocks to 0.
sUMMARY:
malloc() allocates 1 unit(datatype) of memory each time it
is called so to allocate memory for a file read char by
char allocating memory each time for a char till EOF.
calloc allocates sizeof(datatype) bytes to the no of
elements in the file, where by the user can specify the
file size as the second arguement.
char *malloc(sizeof(datatype) )
char *calloc(sizeof(datatype), num of elements)
calloc() is more efficient as memory is allocated in 1
cycle so fewer clock cycles, more faster executiop.

Is This Answer Correct ?    3 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

An integer that indentifies the position of a data item in a sequence of data items a) value b) number c) index d) all of the above

648


What is structure data type in c?

571


What is a char c?

592


Why doesnt long int work?

613


we called a function and passed something do it we have always passed the "values" of variables to the called function. such functions calles are called a) calls by reference b) calls by value c) calls by zero d) none of the above

635






What is Dynamic memory allocation in C? Name the dynamic allocation functions.

558


What is the difference between pure virtual function and virtual function?

652


What is null pointer in c?

595


Is it possible to pass an entire structure to functions?

560


Is this program statement valid? INT = 10.50;

686


How to find a missed value, if you want to store 100 values in a 99 sized array?

816


Why is c called a structured programming language?

677


Why can’t we compare structures?

814


What is the use of a static variable in c?

594


Write an efficient algo and C code to shuffle a pack of cards.. this one was a feedback process until we came up with one with no extra storage.

662