Solved

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

Posted on 2003-10-27
6
227 Views
Last Modified: 2010-04-02
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 ...........

0
Comment
Question by:meow00
6 Comments
 
LVL 19

Expert Comment

by:Dexstar
ID: 9627835
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*
0
 
LVL 86

Expert Comment

by:jkr
ID: 9627877
>>How do I delete the pointer itself ?

You mean the value the pointer has?

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

Author Comment

by:meow00
ID: 9627892
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 !!!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:meow00
ID: 9627918
Actually ... I don't mean the value the pointer has, what I mean is the
"pointer variable p " ... I want to destroy it ... thanks.
0
 
LVL 1

Expert Comment

by:fsign21
ID: 9627953
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
0
 
LVL 19

Accepted Solution

by:
Dexstar earned 50 total points
ID: 9627960
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*
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now