Deallocation of memory using pointers

Deallocation of memory using pointers:

//Dynamic array initialization by using pointers
#include<iostream>
using namespace std;
void main()
{
int a;
cout<<"Enter size of array"<<endl;
cin>>a;
int *b;
b=new int[a];       //this line will allocate the dynamic memory according to the user input
//data input in array
cout<<"enter "<<a<<" element for array"<<endl;
for(int i=0;i<a;i++)
{
cin>>*(b+i);
}

delete [] b; //this command will deallocate the memory locations and you will see garbage values in output

//data output
for(int j=0;j<a;j++)
{
cout<<b[j]<<" ";
}

cout<<endl;
}

Download:

To download this code click on the given link below:

Comments

Popular posts from this blog

How to Run Assembly Language Programming Code In Visual Studio 2019

How to Display Table of any Number in Assembly Language

How to Display Character in Assembly language