0
0
Cprogramming~20 mins

realloc function - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Realloc Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
A10 20 garbage garbage
B10 20 0 0
CRuntime error
D10 20 30 40
Attempts:
2 left
💡 Hint
Remember that realloc keeps the old data intact and allows you to add new elements.
Predict Output
intermediate
2: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;
}
A1 2 3
BAllocation failed
CUndefined behavior
DRuntime error
Attempts:
2 left
💡 Hint
realloc with NULL pointer behaves like malloc.
Predict Output
advanced
2: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;
}
A
Reallocation failed
Segmentation fault
BCompilation error
C
Reallocation succeeded
Value: 42
D
Reallocation failed
Value: 42
Attempts:
2 left
💡 Hint
If realloc fails and returns NULL, the original pointer is lost if overwritten.
Predict Output
advanced
2: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;
}
AUndefined behavior
B1 2 3 4 5
C1 2 3
DRuntime error
Attempts:
2 left
💡 Hint
Shrinking memory keeps the first part intact, but data beyond new size is lost.
Predict Output
expert
2: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?
}
Aptr is NULL
Bptr still points to the original allocated memory
Cptr points to freed memory
Dptr is uninitialized
Attempts:
2 left
💡 Hint
realloc returns NULL on failure but does not free original memory or change original pointer.