OA. free
Free
IBM Core Computer Science Core Computer Science Medium

Question 13 Which of the given below function(s) initializes the allocated memory to "zero"?

IBM technical mcq question, verified with a worked answer. Free to practise - no sign-up.

Which of the given below function(s) initializes the allocated memory to "zero"?

i) malloc()
ii) calloc()
iii) free()

Pick ONE option

Choose one option.
Show answer & explanation
Answer: A. Only (ii)

calloc() allocates memory and explicitly initializes all bytes to zero, making it the only memory allocation function that guarantees zero initialization. malloc() allocates memory but leaves it uninitialized (contains garbage values), and free() deallocates memory rather than initializing it.

Step-by-step Derivation:
Analyze each function:

(i) malloc(size_t size) - Allocates size bytes of uninitialized memory. The allocated block contains garbage values from previous memory use.

(ii) calloc(size_t nitems, size_t size) - Allocates nitems * size bytes of memory AND initializes every byte to 0x00. This is the key difference from malloc().

(iii) free(void *ptr) - Deallocates previously allocated memory. It does not initialize anything; it releases the block back to the heap.

Therefore, only calloc() (option ii) initializes allocated memory to zero. The answer is A.