This example demonstrates how to allocate and deallocate memory dynamically.
cppCopy code#include <iostream>
int main() {
int* array = new int[5]; // Allocate an array of 5 integers
for (int i = 0; i < 5; ++i) {
array[i] = i * 10; // Initialize the array
}
for (int i = 0; i < 5; ++i) {
std::cout << array[i] << " "; // Output: 0 10 20 30 40
}
std::cout << std::endl;
delete[] array; // Deallocate memory
return 0;
}
Leave a Reply