#include <iostream>
#include <cstdlib>
int main() {
// Allocate memory using new operator
int* dynamicArray = new int[5];
// Initialize the allocated memory
for (int i = 0; i < 5; ++i) {
dynamicArray[i] = i * 2;
}
// Adjust memory size using realloc (C) or new (C++)
int newSize = 8;
int resizedArray = (int)realloc(dynamicArray, newSize * sizeof(int));
// Check if reallocation was successful
if (resizedArray != nullptr) {
dynamicArray = resizedArray;
// Initialize the new elements
for (int i = 5; i < newSize; ++i) {
dynamicArray[i] = i * 2;
}
// Output the elements of the resized array
for (int i = 0; i < newSize; ++i) {
std::cout << dynamicArray[i] << " ";
}
// Deallocate memory using delete (C++)
delete[] dynamicArray;
} else {
std::cerr << "Memory reallocation failed!" << std::endl;
}
// Deallocate memory using free (C)
// free(dynamicArray);
return 0;
}