New operator

Hi,
When I run following program...

using namespace std;

int main()

{
int *p =new int;
*p =7;
cout<<p<<endl;
delete p;


*p=9;
cout<<p<<endl;
}

output :
0x897f008
0x897f008

I could not understand why am i getting same address as I have deleted pointer p. I was expecting segementation fault from the program. can anyone provide input.


delete p; doesn't change the pointer in any way, it only releases the chunk of memory it is pointing at.
The pointer still points to the same location afterwards, only that it has become invalid and you may no longer access it.

Sometimes the appropriate page of memory still belongs to program, so trying to access the now invalid memory might not cause a segmentation fault. That's pure chance, though. For all you know, another object could have been constructed there in the meantime.
Topic archived. No new replies allowed.