LogiQuest
Back to Memory Allocation
10

What will be the output of the program?

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *a = malloc(2 * sizeof(int));
    if (!a) return 1;
    a[0] = 1; a[1] = 2;
    int *b = realloc(a, 4 * sizeof(int));
    if (!b) { free(a); return 1; }
    b[2] = 3; b[3] = 4;
    printf("%d\n", b[0] + b[3]);
    free(b);
    return 0;
}
Medium

Discussion (0)

  • No discussion yet. Ask a doubt or share a shortcut.

Log in to join the discussion.