basic question

K

kathy

I have a pointer:

MyClass *p = NULL;
p = new MyClass(...);
....
delete p;

After delete p, does p equal NULL(it is in C++ standard?)? How to
decide if p has been deleted?

The reason I asked this question is that in my project, there are many
code/files use the pointer which I need to determine is it is deleted?
Can I use:

if(p != NULL)
delete p;

I guess somewhere p has been deleted, but p still not NULL(possible?),
the above code might cause problem.
 
B

Ben Pope

kathy said:
I have a pointer:

MyClass *p = NULL;
p = new MyClass(...);

prefer MyClass *p = new MyClass(...);

in fact, prefer:
std::auto_ptr<MyClass> p(new MyClass(...));

If an exception is thrown prior to calling delete p below, your version
has a resource leak. With auto_ptr, you do not have to remember to
delete it, it is automagically deleted when the auto_ptr goes out of scope.

If you need your object to persist beyond the scope of p, you will need
to decide who is in charge of the resource, as p is clearly not. There
are various resource management strategies, and various techniques to
implement that strategy safely.
...
delete p;

After delete p, does p equal NULL(it is in C++ standard?)?
No.

> How to decide if p has been deleted?

Remember, or structure your code such that you cannot access p it after
it has been deleted. You can also systematically set it to 0 after
calling delete, but relying on this is error-prone.
The reason I asked this question is that in my project, there are many
code/files use the pointer which I need to determine is it is deleted?
Can I use:

if(p != NULL)
delete p;

That's ok, as long as you ALWAYS set it to 0 after delete.
I guess somewhere p has been deleted, but p still not NULL(possible?),

That's entirely possible unless you guard against it.
the above code might cause problem.

Of course.

If you find it hard to manage the lifetime of your newly allocated
object, use a smart pointer. There are several available, e.g.,
boost::shared_ptr (or tr1::shared_ptr if you prefer).

This is not a basic question any more. Exception safe resource
management is one of the hardest things to get right.

Read up on RAII (Resource Acquisition Is Initialisation). This is the
technique that smart pointers help you with (with regard to memory, at
least).

Ben Pope
 
B

Bo Persson

kathy said:
I have a pointer:

MyClass *p = NULL;
p = new MyClass(...);
...
delete p;

After delete p, does p equal NULL(it is in C++ standard?)?
No.

How to
decide if p has been deleted?

You have to keep track. :)

Consider what happens here:

MyClass* q = p;

delete p;

How do we know if q is a valid pointer?
The reason I asked this question is that in my project, there are
many
code/files use the pointer which I need to determine is it is
deleted?
Can I use:

if(p != NULL)
delete p;

I guess somewhere p has been deleted, but p still not
NULL(possible?),
the above code might cause problem.

p is not null, until you set it to null. Consider what happens to q
above though!


Also, if p really is NULL, doing a 'delete p' has no effect.

That makes the test a bit silly. :)


The general advice is that code passing a pointer around, has to have
some sort of agreement on who owns the pointer. The owner must manage
the storage.

If you can't arrange this, using some kind of smart pointer might
help. Another way is to store the MyClass objects in a container, like
a std::vector, so that the container can manage the storage for you.



Bo Persson
 
J

Jim Langston

kathy said:
I have a pointer:

MyClass *p = NULL;
p = new MyClass(...);
...
delete p;

After delete p, does p equal NULL(it is in C++ standard?)? How to
decide if p has been deleted?

No. p still points to the same place it did before. Just add:
p = NULL;

afterward.
 
P

Pete C

kathy said:
Can I use:

if(p != NULL)
delete p;

Even if p is NULL, 'delete p' is still safe. You are allowed to delete
a null pointer. Just make sure you don't delete a non-null pointer more
than once.
 
D

davidrubin

kathy said:
I have a pointer:

MyClass *p = NULL;
p = new MyClass(...);
...
delete p;

After delete p, does p equal NULL(it is in C++ standard?)? How to
decide if p has been deleted?

The reason I asked this question is that in my project, there are many
code/files use the pointer which I need to determine is it is deleted?
Can I use:

if(p != NULL)
delete p;

I guess somewhere p has been deleted, but p still not NULL(possible?),
the above code might cause problem.

If I understand you correctly, p is a shared resource among several
objects. Presumably, p is passed into the interface of each of these
objects. If this is the case,

a) auto_ptr will NOT help you whatsoever

b) you cannot test the value of "p" in each object which holds it
because they hold copies of p's value. Thus, setting 'p=0' somewhere
does not affect how other objects view p.

One standard solution is to use some sort of reference-counted
"smart-pointer". However, this type of object is not provided by
standard C++.
 
D

Default User

Jim said:
No. p still points to the same place it did before. Just add:
p = NULL;

afterward.

That does two things.

1. Covers up some errors that should be found and corrected.

2. Gives a false sense of security, because it does nothing for copies
of the pointer.


There's no reason to set the pointer NULL. If your program is correct,
you won't access the invalid pointer.



Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top