#include <iostream>
using namespace std;
int main()
{
int a = 10;
try
{
int *p = new int;
cout<<"p is pointing to address:"<<p<<endl;
cout<<"Value at *p:"<<*p<<endl;
p = &a;
cout<<"a address:"<<&a<<endl;
cout<<"p is pointing to address:"<<p<<endl;
cout<<"Value at *p:"<<*p<<endl;
delete p;
cout<<"a address:"<<&a<<endl;
cout<<"a val:"<<a<<endl;
}
catch(bad_alloc &e)
{
cout<<e.what()<<endl;
}
return 0;
}
In the above example, the address pointed by p is changed to &a. By calling delete, logically the referent of p (deleted the thing pointed to by p) as p is pointing to address of a, now the a's address should be released to the heap.
I m curious to know about, 1. what will happen to the memory allocated by new? will it result in memory leak? 2. Even though this seems to be silly, (any way no harm in registering my thoughts) Will the compiler have any knowledge about the address pointed by p and delete exact address allocated by operator new?