Memory Reallocation

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

int main() {
    // Memory reallocation
    int *dynamicArray;
    int newSize;

    dynamicArray = (int *)malloc(5 * sizeof(int));

    if (dynamicArray == NULL) {
        printf("Memory allocation failed.\n");
        return 1;
    }

    // Use the dynamically allocated array

    printf("Enter the new size of the array: ");
    scanf("%d", &newSize);

    dynamicArray = (int *)realloc(dynamicArray, newSize * sizeof(int));

    if (dynamicArray == NULL) {
        printf("Memory reallocation failed.\n");
        return 1;
    }

    // Use the resized array

    // Free the allocated memory
    free(dynamicArray);

    return 0;
}
chevron_left
chevron_right

Leave a comment

Your email address will not be published. Required fields are marked *

Comment
Name
Email
Website