Bloomberg LP Interview Question
Financial Software DevelopersThere is one more difference.
Malloc - Allocates a single block of memory and returns address of the first allocated byte.
Calloc- Allocates memory as distributed blocks as compared to malloc, initializes all the allocated cblock to 0 and returns address of first byte.
1) malloc return a pointer which is a virutal address, not physical, so MMU will not even do swap for this returned pointer back, therefore very efficient
2) calloc return a pointer which is also a virtual address, but calloc do zero out the physical memory , therefore page in/out will happen and thus slow down the speed
There are two differences.
- UB_Green January 06, 2011First, is in the number of arguments. Malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments (number of variables to allocate memory, size in bytes of a single variable).
Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.