#include <iostream>
#include <cstdlib>
int main() {
// Allocate memory for an array of 10 integers using malloc()
int *arr = (int*)malloc(10 * sizeof(int));
// Check if the memory was successfully allocated
if (arr == NULL) {
std::cerr << "Error allocating memory" << std::endl;
return 1;
}
// Initialize the elements of the array to 0
for (int i = 0; i < 10; i++) {
arr[i] = 0;
}
// Print the elements of the array
std::cout << "Elements of the array allocated with malloc(): ";
for (int i = 0; i < 10; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
// Deallocate the memory
free(arr);
// Allocate memory for an array of 10 integers using calloc()
int *arr2 = (int*)calloc(10, sizeof(int));
// Check if the memory was successfully allocated
if (arr2 == NULL) {
std::cerr << "Error allocating memory" << std::endl;
0 Comments