Challenge - 5 Problems
Realloc Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of realloc with increased size
What is the output of this C program using
realloc to increase the size of an integer array?C
#include <stdio.h> #include <stdlib.h> int main() { int *arr = malloc(2 * sizeof(int)); arr[0] = 10; arr[1] = 20; arr = realloc(arr, 4 * sizeof(int)); arr[2] = 30; arr[3] = 40; for (int i = 0; i < 4; i++) { printf("%d ", arr[i]); } free(arr); return 0; }
Attempts:
2 left
💡 Hint
Remember that realloc keeps the old data intact and allows you to add new elements.
✗ Incorrect
The realloc call increases the allocated memory size. The original two elements remain unchanged, and the new elements can be assigned and printed correctly.
❓ Predict Output
intermediate2:00remaining
Behavior of realloc with NULL pointer
What will this C program print when using
realloc with a NULL pointer?C
#include <stdio.h> #include <stdlib.h> int main() { int *ptr = NULL; ptr = realloc(ptr, 3 * sizeof(int)); if (ptr == NULL) { printf("Allocation failed\n"); } else { ptr[0] = 1; ptr[1] = 2; ptr[2] = 3; for (int i = 0; i < 3; i++) { printf("%d ", ptr[i]); } free(ptr); } return 0; }
Attempts:
2 left
💡 Hint
realloc with NULL pointer behaves like malloc.
✗ Incorrect
When realloc is called with a NULL pointer, it behaves like malloc and allocates new memory. So the program assigns values and prints them successfully.
❓ Predict Output
advanced2:00remaining
Output when realloc fails
What happens when
realloc fails to allocate memory and you assign its result directly to the original pointer?C
#include <stdio.h> #include <stdlib.h> int main() { int *ptr = malloc(1000000000 * sizeof(int)); if (ptr == NULL) { printf("Initial allocation failed\n"); return 1; } ptr[0] = 42; ptr = realloc(ptr, 1000000000000 * sizeof(int)); if (ptr == NULL) { printf("Reallocation failed\n"); } else { printf("Reallocation succeeded\n"); } printf("Value: %d\n", ptr[0]); free(ptr); return 0; }
Attempts:
2 left
💡 Hint
If realloc fails and returns NULL, the original pointer is lost if overwritten.
✗ Incorrect
When realloc fails, it returns NULL but the original pointer is overwritten, causing a memory leak and ptr becomes NULL. Accessing ptr[0] causes a segmentation fault.
❓ Predict Output
advanced2:00remaining
Effect of realloc shrinking memory
What is the output of this program that uses
realloc to shrink an allocated array?C
#include <stdio.h> #include <stdlib.h> int main() { int *arr = malloc(5 * sizeof(int)); for (int i = 0; i < 5; i++) { arr[i] = i + 1; } arr = realloc(arr, 3 * sizeof(int)); for (int i = 0; i < 3; i++) { printf("%d ", arr[i]); } free(arr); return 0; }
Attempts:
2 left
💡 Hint
Shrinking memory keeps the first part intact, but data beyond new size is lost.
✗ Incorrect
After realloc shrinks the array, only the first 3 elements remain valid. Printing beyond that would be invalid.
❓ Predict Output
expert2:00remaining
Value of pointer after realloc with NULL return
Consider this code snippet. What is the value of
ptr after the realloc call if it fails?C
int *ptr = malloc(10 * sizeof(int)); // Assume malloc succeeded int *new_ptr = realloc(ptr, 1000000000000 * sizeof(int)); if (new_ptr == NULL) { // realloc failed // What is the value of ptr here? }
Attempts:
2 left
💡 Hint
realloc returns NULL on failure but does not free original memory or change original pointer.
✗ Incorrect
If realloc fails, it returns NULL and leaves the original pointer unchanged. So ptr still points to the original memory block.