Dynamic Heap Example in C++


This program creates an integer pointer p and dynamically allocates an array of 5 integers using the new operator. The second element of the array is then assigned the value 20 and printed using cout. The delete[] operator is used to free the memory allocated by new, and the pointer p is set to nullptr.

Source Code

#include<iostream>
using namespace std;
int main()
{
    int *p=new int[5];
    p[0]=10;
    p[1]=20;
    cout <<p[1]<<endl;
    delete []p;
    p=nullptr;
    return 0;
}
To download raw file Click Here

Output

20

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples