33
Total Posts
3
Categories
24/7
Updated

C++

Write a C++ program to allocate memory using new operator.

Published on Dec 22, 2025
#include <iostream>
using namespace std;

int main() {
    int *ptr;

    // Allocating memory dynamically
    ptr = new int;

    // Assigning value
    *ptr = 25;

    // Displaying value
    cout << "Value stored in dynamically allocated memory: " << *ptr << endl;

    // Deallocating memory
    delete ptr;

    return 0;
}

Documentation

Output

Value stored in dynamically allocated memory: 25

 

Important Points

  • new allocates memory at runtime

  • Memory allocated by new must be freed using delete

  • ptr stores the address of dynamically allocated memory

  • *ptr accesses the value stored in memory