Dynamic Memory Allocation using new and delete Operators in c++





This video tutorial teaches you how to do dynamic memory allocation in c++ using new and delete operators.

check out what is dynamic memory allocation and what is the need of it at
https://www.youtube.com/watch?v=X_7VIkTPHBo

you will learn how to use new and delete operators in c++, how new and delete operator works in c++, how to allocate memory using new and how to deallocate or free memory using free in c++ with examples.

source code for this tutorial

#include <iostream>

using namespace std;

int main()
{
    int *pointer = nullptr;
    cout << "how many items u are gonna enter"<<endl;
    int input;
    cin >> input;

    pointer = new int[input];

    int temp;

    for(int counter =0; counter < input ; counter++){
        cout << "enter the item "<<counter+1<<endl;
        cin >> temp;
        *(pointer+counter) =  temp;
    }

    cout << "the items you have entered are"<<endl;
        for(int counter =0; counter < input ; counter++){
        cout << counter+1 << "  item is  "<<*(pointer+counter)<<endl;

    }

    delete []pointer;

    return 0;
}

 
Back To Top