Difficulty: Easy
Correct Answer: malloc(size) allocates an uninitialised block of the given size in bytes, while calloc(n, size) allocates space for n elements of the given size and initialises all bits in the block to zero.
Explanation:
Introduction / Context:
The C standard library provides several functions for allocating heap memory, and understanding the differences between them is important for writing correct and efficient programs. malloc and calloc are two commonly used functions that appear similar but behave differently in terms of how they compute the allocation size and how they initialise the allocated block. This question asks you to recall their key distinctions.
Given Data / Assumptions:
Concept / Approach:
malloc(size) simply reserves a block of at least size bytes and leaves its contents indeterminate; the bytes may contain leftover data from previous uses of that memory region. It is the programmer's responsibility to initialise the memory before use. calloc(n, size) computes n * size bytes and then allocates that block, setting all bits in the memory region to zero. For integer and pointer types, that effectively means values are zero or NULL. For floating point and more complex types, bitwise zero is usually but not always the same as numerical zero, but in practice it is treated that way on common platforms.
Step-by-Step Solution:
Step 1: Describe malloc: it has the prototype void *malloc(size_t size) and returns uninitialised memory.
Step 2: Describe calloc: it has the prototype void *calloc(size_t n, size_t size) and returns memory where all bytes have been set to zero.
Step 3: Recognise that calloc internally multiplies n by size to determine the total number of bytes requested.
Step 4: Note that both functions require error checking for NULL return values and that the returned memory must eventually be freed with free.
Step 5: Choose the option that mentions both the uninitialised versus zero initialised behaviour and the parameter difference.
Verification / Alternative check:
You can test the difference by allocating an array of int with malloc and printing its contents; you will likely see unpredictable values. With calloc, the same experiment typically prints zeros. Documentation for C libraries emphasises that calloc performs an initialisation step equivalent to memset to zero, whereas malloc does not. This confirms the explanation and clarifies that both allocate on the heap, not on the stack.
Why Other Options Are Wrong:
Option B incorrectly claims that malloc allocates on the stack, which is not true; stack storage is controlled by automatic variables, not malloc. Option C states that calloc always succeeds, which is false; both functions can fail if memory is exhausted. Option D denies any difference, ignoring the distinct parameter lists and the important initialisation behaviour of calloc.
Common Pitfalls:
One pitfall is to rely on malloc to provide zeroed memory, which can lead to subtle bugs when uninitialised values are used. Another is to forget that calloc's multiplication of n and size can overflow size_t, potentially resulting in a smaller allocation than expected. In performance critical code, programmers sometimes prefer malloc plus explicit initialisation to avoid the overhead of zeroing large blocks when not necessary.
Final Answer:
malloc(size) allocates an uninitialised block of size bytes, while calloc(n, size) allocates space for n elements of the given size and sets all bits in the allocated block to zero.
Discussion & Comments