i_hate_toms
New Member
Hello everyone, hope you're doing good 
Please take a look at the following program:-
Why do we need to learn this weird process? Why not write
int x;
cin>>x;
???
Thnx..

Please take a look at the following program:-
What's going on at "ptr=new int" ?//INDEPENDENT MODULE:dynamic memory allocation with keyword "new"
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int *ptr; //declaring integer pointer
cout<<"Attempting dynamic memory allocation now...\n";
ptr=new int;//attempting to allocate memory for the integer using keyword new
if(!ptr)//check if allocation successful, NULL is returned if allocation fails
{
cout<<"Memory Allocation Failed!";//Message displayed if allocation failed
}
else //enter this block only if allocation has succeeded, ie,"ptr!=NULL"
{
cout<<"SUCCESS: Memory allocated for int object\n";//Display message confirming allocation
cout<<"Enter integer value ";//ask end user to enter integer value
cin>>*ptr; //accept a value and store at the memory address pointed by integer type pointer variable "*ptr"
int x=*ptr;//declare integer variable "x" and assign the user-entered value to x, using indirection
cout<<"Value "<<x<<" assigned to memory address "<<&x<<" by indirection\n"; //display result
cout<<"Press any key to exit...";//Graceful Shutdown Prompt
getch();
}
}
Why do we need to learn this weird process? Why not write
int x;
cin>>x;
???
Thnx..