The realloc function changes the size of a previously allocated memory block. It can increase or decrease the size without losing the existing data up to the minimum of old and new sizes.
Click to reveal answer
intermediate
What happens if realloc cannot enlarge the existing memory block in place?
If realloc cannot extend the current block, it allocates a new block of the requested size, copies the old data to the new block, frees the old block, and returns a pointer to the new block.
Click to reveal answer
intermediate
What does realloc(ptr, 0) do?
Calling realloc with size 0 is implementation-defined: it may either free the memory pointed to by ptr and return NULL, or return a unique pointer that should not be dereferenced.
Click to reveal answer
intermediate
How should you safely use realloc to avoid memory leaks?
Always assign the result of realloc to a temporary pointer first. If it returns NULL, the original pointer is still valid. Only update the original pointer if realloc succeeds.
Click to reveal answer
beginner
What header file must be included to use realloc?
You must include <stdlib.h> to use the realloc function in C.
Click to reveal answer
What does realloc do if the new size is smaller than the old size?
AIt reduces the memory block size and keeps the first part of the data.
BIt frees the old block and returns NULL.
CIt increases the memory block size.
DIt leaves the memory block unchanged.
✗ Incorrect
realloc reduces the block size and preserves the data up to the new smaller size.
If realloc fails to allocate new memory, what happens to the original pointer?
AIt remains valid and unchanged.
BIt points to freed memory.
CIt becomes NULL.
DIt points to the new memory block.
✗ Incorrect
The original pointer remains valid if realloc fails; only the returned pointer is NULL.
Which header file is required to use realloc?
A<code><stdio.h></code>
B<code><stdlib.h></code>
C<code><string.h></code>
D<code><memory.h></code>
✗ Incorrect
realloc is declared in <stdlib.h>.
What is the correct way to avoid losing the original pointer when using realloc?
AUse <code>malloc</code> instead of <code>realloc</code>.
BAssign <code>realloc</code> result directly to the original pointer.
CCall <code>free</code> before <code>realloc</code>.
DUse a temporary pointer to store <code>realloc</code> result first.
✗ Incorrect
Using a temporary pointer prevents losing the original pointer if realloc fails.
What does realloc(NULL, size) do?
AReturns NULL.
BFrees memory.
CActs like <code>malloc(size)</code>.
DCauses a runtime error.
✗ Incorrect
Passing NULL to realloc behaves like malloc and allocates new memory.
Explain how the realloc function works and when you would use it.
Think about changing the size of a box without losing what is inside.
You got /4 concepts.
Describe the safe way to use realloc to avoid memory leaks or losing data.
Imagine you want to move your stuff to a bigger box but keep the old box until the new one is ready.
You got /4 concepts.
Practice
(1/5)
1. What does the realloc function do in C programming?
easy
A. It allocates a new memory block without preserving old data.
B. It frees the allocated memory block immediately.
C. It changes the size of a previously allocated memory block, preserving its content.
D. It copies data from one memory block to another.
Solution
Step 1: Understand the purpose of realloc
The realloc function is used to resize an existing memory block allocated by malloc or calloc.
Step 2: Check data preservation behavior
realloc preserves the existing data up to the minimum of old and new sizes.
Final Answer:
It changes the size of a previously allocated memory block, preserving its content. -> Option C
Quick Check:
realloc resizes memory and keeps data [OK]
Hint: Remember: realloc resizes and keeps old data safe [OK]
Common Mistakes:
Thinking realloc frees memory without resizing
Assuming realloc always allocates new memory without preserving data
Confusing realloc with memcpy or free
2. Which of the following is the correct syntax to resize a memory block pointed by ptr to hold 50 integers using realloc?
easy
A. ptr = realloc(ptr, 50 * sizeof(int));
B. ptr = realloc(sizeof(int), 50 * ptr);
C. ptr = realloc(50, ptr * sizeof(int));
D. ptr = realloc(ptr, 50);
Solution
Step 1: Understand realloc parameters
realloc takes a pointer and the new size in bytes. To hold 50 integers, size must be 50 times size of int.
Step 2: Check each option's correctness
ptr = realloc(ptr, 50 * sizeof(int)); correctly multiplies 50 by sizeof(int). Others misuse parameters or sizes.
Final Answer:
ptr = realloc(ptr, 50 * sizeof(int)); -> Option A
Quick Check:
Size in bytes = count * sizeof(type) [OK]
Hint: Multiply count by sizeof(type) for realloc size [OK]
A. Calling free(ptr) after ptr is NULL does not free the original memory block.
B. Missing check for malloc failure before realloc.
C. Incorrect size calculation in realloc.
D. No error; code is correct.
Solution
Step 1: Understand realloc return and NULL check
If realloc returns NULL, original pointer is lost if not saved separately, and ptr becomes NULL.
Step 2: Analyze free(ptr) after NULL assignment
Calling free(ptr) after ptr is NULL does not free the original memory block, leading to memory leak.
Final Answer:
Calling free(ptr) after ptr is NULL does not free the original memory block. -> Option A
Quick Check:
Check realloc result before overwriting pointer [OK]
Hint: Save realloc result before overwriting pointer [OK]
Common Mistakes:
Not checking realloc return before assignment
Freeing NULL pointer thinking it frees old memory
Ignoring malloc failure before realloc
5. You have a dynamic array of integers allocated with malloc. You want to safely double its size using realloc without losing data or causing memory leaks. Which approach is correct?
hard
A. ptr = malloc(2 * current_size); memcpy(ptr, old_ptr, current_size); free(old_ptr);
B. ptr = realloc(ptr, 2 * current_size); // directly assign realloc result
C. free(ptr); ptr = malloc(2 * current_size); // allocate new memory after freeing
D. int *temp = realloc(ptr, 2 * current_size); if (temp) ptr = temp; else handle error;
Solution
Step 1: Understand safe realloc usage
Directly assigning realloc result to original pointer risks losing original memory if realloc fails.
Step 2: Use temporary pointer to check realloc success
Assign realloc to temp pointer, check if not NULL, then update original pointer. Else handle error safely.
Final Answer:
int *temp = realloc(ptr, 2 * current_size); if (temp) ptr = temp; else handle error; -> Option D
Quick Check:
Use temp pointer to avoid memory leaks on realloc failure [OK]
Hint: Use temp pointer for realloc to avoid losing original memory [OK]
Common Mistakes:
Overwriting pointer before checking realloc result
Freeing pointer before realloc causing data loss
Not handling realloc failure properly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.