Raw Dynamic Allocation: new & delete

🔴 Advanced

📖 Definition

The new operator allocates heap memory and calls the object's constructor. The delete operator calls the object's destructor and frees heap memory. For arrays allocated via new T[N], matching delete[] must be invoked to prevent undefined behavior.

💡 Code Example

#include <iostream>

int main() {
    int* arr = new int[3]{100, 200, 300}; // Array new

    for (int i = 0; i < 3; ++i) {
        std::cout << arr[i] << " ";
    }
    std::cout << "\n";

    delete[] arr; // Array delete
    arr = nullptr;
    return 0;
}