473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

basic question

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.

Feb 3 '06 #1
6 1583
kathy wrote:
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
--
I'm not just a number. To many, I'm known as a string...
Feb 3 '06 #2

"kathy" <yq*****@yahoo.com> skrev i meddelandet
news:11*********************@g47g2000cwa.googlegro ups.com...
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
Feb 3 '06 #3

"kathy" <yq*****@yahoo.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
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.
Feb 3 '06 #4
kathy wrote:
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.

Feb 3 '06 #5

kathy wrote:
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++.

Feb 3 '06 #6
Jim Langston wrote:

"kathy" <yq*****@yahoo.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
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.


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

Feb 4 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: pauldepstein | last post by:
I am reading Grimshaw and Ortega's "C++ and Numerical Methods." They construct a vector class which contains the variable vec, a float* variable where the length of the array (number of...
6
by: DH | last post by:
I have a VERY basic question about figuring database size. I've inherited a database which is generally similar to this basic one: Item, Red, Blue, Green, Yellow (text), (int),(int),(int),(int)...
9
by: Malcolm | last post by:
After some days' hard work I am now the proud possessor of an ANSI C BASIC interpreter. The question is, how is it most useful? At the moment I have a function int basic(const char *script,...
4
by: Ramesh | last post by:
hi, Let me ask some basic questions. Can anybody explain me about the following questions: 1. When we have to create sn key? Whenever we compiled Component we have to create or it is a one time...
13
by: Pete | last post by:
I'm cross posting from mscom.webservices.general as I have received no answer there: There has been a number of recent posts requesting how to satisfactorily enable BASIC authorization at the...
5
by: Aussie Rules | last post by:
Hi, Having a mental block on this one. Have done it before but can't rack my brain on how... I have an object, with a bunch on property, and I add that object to a combo box. I want the...
4
by: MikeB | last post by:
I've been all over the net with this question, I hope I've finally found a group where I can ask about Visual Basic 2005. I'm at uni and we're working with Visual Basic 2005. I have some books, ...
1
by: frankhanretty | last post by:
Do I have to install Visual basic on the remote terminals as I did on the server? I have an visual basic 5 application running fine on my client's server and he is now networked. He wants to run the...
4
by: Chris Asaipillai | last post by:
Hi there My compay has a number of Visual Basic 6 applications which are front endeed onto either SQL Server or Microsoft Access databases. Now we are in process of planning to re-write these...
3
by: Scott Stark | last post by:
Hello, I'm trying to get a better handle on OOP programming principles in VB.NET. Forgive me if this question is sort of basic, but here's what I want to do. I have a collection of Employee...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.