De-allocation of a pointer does not work properly

Dear all,
I am learning about pointers in C++. I practice creating a pointer then delete it when I do not need. I use "delete pointer". However the following code seems does not work as expected.
Please help me to point out it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include<iostream>
using namespace std;
int main(){
    int *ptr;
    ptr=new int;
    if (ptr==NULL){
        cout<<"Memory allocation failure!"<<endl;
        return -1;
    }
    cout<<"Pointer before deleting:"<<ptr<<endl;
    //Delete the pointer
    delete ptr;
    //Deference it
    *ptr=3;
    cout<<"The pointer is still here [???]:"<<ptr<<endl;
    }


The output of the snippet:
https://drive.google.com/file/d/0BxDjLP_mwwuWSXBpQXdVRVA3TFU/view?usp=sharing
1
2
3
4
5
    ptr=new int;
    if (ptr==NULL){
        cout<<"Memory allocation failure!"<<endl;
        return -1;
    }


this is incorrect the new operator does not return a null pointer on failure it throws an exception.


calling delete on a pointer does not change the address of the pointer. You can point to a memory address you don't own.
Topic archived. No new replies allowed.