In C programming, what is the difference between realloc() and free() when managing dynamic memory on the heap?

Difficulty: Easy

Correct Answer: realloc() changes the size of an already allocated block (possibly moving it) while preserving existing data, whereas free() simply releases a previously allocated block back to the heap.

Explanation:


Introduction / Context:
Dynamic memory management in C relies on functions like malloc(), calloc(), realloc(), and free(). This question focuses on understanding the specific roles of realloc() and free(), which is a common interview topic for C and systems programming. Knowing when to resize vs when to release memory is crucial for avoiding leaks and undefined behaviour.


Given Data / Assumptions:

  • Memory is acquired from the heap using malloc(), calloc(), or realloc().
  • We have a valid pointer returned by a previous allocation call.
  • We need either to change the size of the allocation or completely release it.


Concept / Approach:
realloc() is used when you already have a dynamically allocated block and want to change its size. It may grow or shrink the block and can move the block to a new location if necessary, copying the existing data up to the smaller of the old and new sizes. free(), on the other hand, does not resize or copy anything. It simply releases the entire block associated with the pointer, returning that memory to the heap allocator so it can be reused.


Step-by-Step Solution:
Step 1: Allocate memory: ptr = malloc(n * sizeof(int)); now ptr refers to a heap block. Step 2: If you later need more or less space, call ptr = realloc(ptr, new_size); this may return a new pointer and copies existing contents as needed. Step 3: If you no longer need the memory at all, call free(ptr); this marks the block as free in the allocator. Step 4: After free(), using the pointer without reassigning it leads to undefined behaviour. Step 5: Therefore realloc() is for resizing an existing allocation, while free() is for completely releasing it.


Verification / Alternative check:
You can confirm the behaviour experimentally by allocating an array, filling it with values, calling realloc() with a larger size, and checking that the original values are preserved. After calling free(), any attempt to read or write through the old pointer can crash or corrupt the program, confirming that the block is no longer valid. Using tools like valgrind also shows that free() removes leaks while realloc() may still require a final free().


Why Other Options Are Wrong:
Option B is incorrect because neither realloc() nor free() exists only to zero memory; only calloc() initializes memory to zero. Option C reverses responsibilities and gives free() behaviour that it does not have. Option D is wrong because both functions work only with dynamically allocated heap memory, not with stack or global variables.


Common Pitfalls:
Common mistakes include forgetting to assign the return value of realloc() back to the pointer, losing the original pointer on failure, or calling free() on memory that was never dynamically allocated. Another frequent error is using the pointer after free(), which causes undefined behaviour. Correct use of realloc() and free() helps avoid memory leaks and crashes in C programs.


Final Answer:
The correct statement is that realloc() changes the size of an already allocated block (possibly moving it) while preserving existing data, whereas free() simply releases a previously allocated block back to the heap.

More Questions from Technology

Discussion & Comments

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