Calloc C++

#include <iostream>
#include <cstdlib>

int main() {
    // Declare variables
    int n;

    // Get user input for the number of elements
    std::cout << "Enter the number of elements: ";
    std::cin >> n;

    // Allocate memory for an array of n integers using calloc
    int arr = (int)calloc(n, sizeof(int));

    // Check if memory allocation is successful
    if (arr == nullptr) {
        std::cerr << "Memory allocation failed." << std::endl;
        return 1; // Exit program with an error code
    }

    // Initialize array elements
    for (int i = 0; i < n; ++i) {
        arr[i] = i + 1;
    }

    // Display the initialized array
    std::cout << "Initialized array elements: ";
    for (int i = 0; i < n; ++i) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;

    // Deallocate the allocated memory using free
    free(arr);

    return 0; // Exit program successfully
}