How do I delete a pointer (a pointer to an object) ?

meow00
meow00 used Ask the Experts™
on
Hi C++ experts,

   I have some questions in the following code :
--------------------------------
  class A{
  } ;

  A *p = new A ;
  delete p ;
---------------------------------
  the "delete p" deletes the object "new A", but the pointer p is still there. How do I delete the pointer itself ?
Also, will p be destroyed automatically if it goes out of scope ?
Thanks a lot !!!

meow ...........

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
meow00:

> the "delete p" deletes the object "new A", but the pointer p is still there.
> How do I delete the pointer itself ?
> Also, will p be destroyed automatically if it goes out of scope ?

Yeah, you don't have to "delete" anything unless it is created by "new".  A* p is creating on the stack without "new", so it will just be destroyed when it goes out of scope.  FYI:  You have to "delete p" before p goes out of scope or else there will be no way to delete it.

Hope that helps,
Dex*
jkr
Top Expert 2012

Commented:
>>How do I delete the pointer itself ?

You mean the value the pointer has?

#define SAFE_DELETE(x) { delete x; x = NULL;}

Author

Commented:
I see ....
and I am wondering that : is there a certain kind of "destructor" which can be called explicitly to delete the pointer p ? or actually there is no way to do that ? we can only wait until it goes out of scope or the program terminates ?
Thanks a lot !!!
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Author

Commented:
Actually ... I don't mean the value the pointer has, what I mean is the
"pointer variable p " ... I want to destroy it ... thanks.

Commented:
The pointer p itself will be destroyed automatically if it goes out of scope.

The allocated object will be not deallocated if it goes out of scope, unless you call delete p.

We had a similar question recenly:
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20772740.html
Commented:
meow00:

> Actually ... I don't mean the value the pointer has, what I mean is the
> "pointer variable p " ... I want to destroy it ... thanks.

Like I said before.  You can't "destroy it", except to let it fall out of scope.  One way to do is, is to put the use of it inside of {}'s.  Those will tell your compiler to control the scope more tightly.  For example:

void Whatever()
{
     int i = 5;

     {
          int* p = new int;

          *p = i;
   
          delete p;
     } // p will fall out of scope here

     // Try to use p here, and you'll get an error!
}

Dex*

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial