Difficulty: Easy
Correct Answer: Yes. Use realloc on the existing pointer, for example int *tmp = realloc(p, newCount * sizeof *p); if (tmp) { p = tmp; }.
Explanation:
Introduction / Context:
Arrays allocated with malloc often need to grow or shrink as data structures change size at run time. The C standard library provides a specific function, realloc, to support resizing previously allocated blocks while preserving their existing contents when possible. This question asks whether such resizing is legal and how to use realloc safely in idiomatic C code.
Given Data / Assumptions:
Concept / Approach:
The realloc function takes a pointer to an existing block and a new size in bytes. It may either extend the block in place or allocate a new block, copy the old contents up to the minimum of old and new sizes and then free the old block. If the requested size is zero, realloc may behave like free. Because realloc can return NULL on failure without freeing the original block, it is safer to assign its result to a temporary pointer, check for success, and only then update the original pointer variable.
Step-by-Step Solution:
Step 1: Compute the new size in bytes as newCount * sizeof *p for an array of the same element type.
Step 2: Call realloc using a temporary pointer: int *tmp = realloc(p, newCount * sizeof *p);.
Step 3: Check whether tmp is not NULL. If it is NULL, the reallocation failed and p still points to the original block.
Step 4: If tmp is non NULL, assign p = tmp; so that p now points to the resized block.
Step 5: Continue to use p as before, remembering to free it exactly once when it is no longer needed.
Verification / Alternative check:
The C standard specifies that if realloc fails, it returns NULL and leaves the original block untouched, which is why overwriting p directly would lose the pointer and leak memory. Testing in small programs shows that reallocation can often grow arrays without copying if there is space, or else returns a new pointer with preserved contents. This confirms that realloc is the intended mechanism for dynamically resizing arrays in C.
Why Other Options Are Wrong:
Option B is incorrect because realloc is specifically designed to change the size of a dynamically allocated block. Option C suggests that changing the pointer value directly can grow memory, which has no basis in the C runtime and leads to undefined behaviour. Option D frees and reallocates without copying, which loses all existing data and therefore does not match the usual requirement when increasing array size.
Common Pitfalls:
A common mistake is to write p = realloc(p, newSize); without checking for failure. If realloc returns NULL, the original block is leaked because its address has been overwritten. Another pitfall is to allocate the wrong number of bytes by forgetting to multiply by sizeof element, leading to buffer overflows or wasted memory. Using sizeof *p and a temporary pointer pattern reduces these risks in real applications and exam answers.
Final Answer:
Yes, you can grow a dynamically allocated array by calling realloc, for example int *tmp = realloc(p, newCount * sizeof *p); and if tmp is not NULL, then assigning p = tmp;.
Discussion & Comments