logo

How to use realloc in C

#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int* arr = malloc(sizeof(int) * 100); printf("%p\n", arr); // ... int* res = realloc(arr, sizeof(int) * 10000000000); printf("%p\n", res); if (res == NULL) { fprintf(stderr, "Failed to realloc\n"); free(arr); return 1; } arr = res; for (int i = 0; i < 8; i++) { arr[i] = 11; } for (int i = 0; i < 8; i++) { printf("%d ", arr[i]); } free(arr); return 0; }