Dynamic Memory Allocation

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;
}

Comments

Leave a Reply

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