#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
// Step 1: Declare a void pointer
void* ptr;
// Step 2: Allocate memory for an integer
ptr = malloc(sizeof(int));
// Step 3: Check if memory allocation is successful
if (ptr == nullptr) {
cerr << "Memory allocation failed." << endl;
return 1;
}
// Step 4: Cast the void pointer to an integer pointer
int intPtr = static_cast<int>(ptr);
// Step 5: Assign a value to the allocated integer
*intPtr = 42;
// Step 6: Display the value of the allocated integer
cout << "The value of the allocated integer is: " << *intPtr << endl;
// Step 7: Deallocate the allocated memory
free(ptr);
return 0;
}