Dynamic Memory Allocation

This example demonstrates how to allocate and deallocate memory dynamically.

cppCopy code#include <iostream>

int main() {
int* array = new int&#91;5]; // Allocate an array of 5 integers
for (int i = 0; i &lt; 5; ++i) {
    array&#91;i] = i * 10; // Initialize the array
}
for (int i = 0; i &lt; 5; ++i) {
    std::cout &lt;&lt; array&#91;i] &lt;&lt; " "; // Output: 0 10 20 30 40
}
std::cout &lt;&lt; std::endl;
delete&#91;] array; // Deallocate memory
return 0;
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *