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
What is return type in c?
What is the difference between strcpy() and memcpy() function in c programming?
Write the test cases for checking a variable having value in range -10.0 to +10.0?
What is a method in c?
Write a function which takes as parameters one regular expression(only ? and * are the special characters) and a string and returns whether the string matched the regular expression.
What is local and global variable in c?
What is void main () in c?
What is difference between static and global variable in c?
ATM machine and railway reservation class/object diagram
What is the use of header files?
How can you find the exact size of a data type in c?
How many keywords (reserve words) are in c?
What is array of structure in c programming?
How can I find the modification date and time of a file?
What is the concatenation operator?