In C memory management, what is the difference between realloc() and free() when working with dynamically allocated blocks?

Difficulty: Medium

Correct Answer: realloc() changes the size of an existing allocated block, while free() releases an allocated block back to the heap

Explanation:


Introduction / Context:
In C programming, dynamic memory management is handled using functions such as malloc(), calloc(), realloc(), and free(). Understanding the specific role of each function is critical for writing efficient and safe programs. Two commonly compared functions are realloc() and free(). Both operate on memory previously obtained from malloc(), calloc(), or realloc(), but they perform very different actions. Interview questions frequently test whether you know when to resize memory, when to release memory completely, and what happens to pointers after these operations.


Given Data / Assumptions:
- We are working in C with dynamically allocated memory on the heap.
- Memory blocks have already been allocated using malloc(), calloc(), or realloc().
- The question asks specifically about how realloc() differs from free().
- No numeric calculation is required; the focus is on behaviour and correct usage.


Concept / Approach:
The realloc() function adjusts the size of an existing allocated block. It can increase or decrease the size. If there is enough space after the current block, it may resize in place. Otherwise, it allocates a new block of the requested size, copies the existing data to the new block, and returns a pointer to the new location. The free() function, in contrast, simply releases a previously allocated block back to the heap so that it can be reused by future allocations. After calling free(), the old pointer becomes invalid and should not be dereferenced. The correct explanation must describe realloc() as resizing an existing allocation and free() as releasing it.


Step-by-Step Solution:
Step 1: Recall that realloc(ptr, newSize) expects a pointer returned earlier by malloc(), calloc(), or realloc(), and a new size in bytes.Step 2: Understand that realloc() attempts to change the size of the block. If possible, it extends or shrinks the block in place; otherwise, it allocates a new block, copies data, and frees the old block internally.Step 3: Remember that free(ptr) releases the memory so that the heap manager can reuse it. After calling free(), the pointer should not be used unless it is set to NULL.Step 4: Compare the options and identify the one that states that realloc() changes the size of an existing block while free() releases a block back to the heap.Step 5: Select option A because it describes exactly this difference and no other option matches the correct behaviour.


Verification / Alternative check:
You can confirm the difference with a simple example. First call malloc() to allocate memory for 10 integers. Then call realloc() on that pointer to change it to 20 integers. The pointer you get back may be the same or different, but you still have valid data and a valid block. Now call free() on that pointer. After free(), any further attempt to use that memory leads to undefined behaviour. This sequence shows that realloc() is for resizing a live allocation, while free() ends the life of an allocation completely.


Why Other Options Are Wrong:
Option B incorrectly claims that realloc() allocates a block for the first time, but new allocations are handled by malloc() or calloc(), not by realloc(). Option C suggests that realloc() releases all program memory, which is completely incorrect and far too broad in scope. Option D implies that realloc() works on stack memory; in reality, both realloc() and free() operate only on heap memory, never on stack frames. These descriptions do not match the real responsibilities of realloc() and free().


Common Pitfalls:
A frequent error is forgetting to store the pointer returned by realloc() and continuing to use the old pointer, which may have been freed internally. Another pitfall is trying to free() the same pointer twice, causing undefined behaviour. Developers must also avoid calling realloc() or free() on pointers that were not obtained from dynamic allocation functions. Finally, it is important to check for NULL returns from realloc(), because failure to allocate a larger block should be handled gracefully to prevent memory leaks or program crashes.


Final Answer:
Correct answer: realloc() changes the size of an existing allocated block, while free() releases an allocated block back to the heap

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion