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

write a c program to do the following: a) To find the area of a triangle. b) To convert the temperature from Fahrenheit to Celsius. c) To convert the time in hours : minutes : seconds to seconds.

1520


What is a node in c?

552


What is the advantage of a random access file?

642


What is the meaning of typedef struct in c?

598


What is header file definition?

573






What is the difference between a free-standing and a hosted environment?

644


How can I check whether a file exists? I want to warn the user if a requested input file is missing.

658


What is the difference between single charater constant and string constant?

625


Can you define which header file to include at compile time?

591


Do you have any idea how to compare array with pointer in c?

607


What is meant by initialization and how we initialize a variable?

590


How to write a multi-statement macro?

628


Explain what is a 'null pointer assignment' error? Explain what are bus errors, memory faults, and core dumps?

646


What is #include conio h?

598


What are the types of data types and explain?

675