473,545 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1592
kathy wrote:
I have a pointer:

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

in fact, prefer:
std::auto_ptr<M yClass> 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_p tr (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******** *************@g 47g2000cwa.goog legroups.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******** *************@g 47g2000cwa.goog legroups.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******** *************@g 47g2000cwa.goog legroups.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
2471
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 components in the vector) is given by the variable name veclength. That is what I _do_ understand. What I don't understand is the coding for the...
6
5104
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) box, 1,0,0,2 hat, 0,0,0,1 car, 3,0,0,0 This format leads to a lot of zeros in the rows which take up a lot of
9
2229
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, FILE *in, FILE *out, FILE *err); It returns 0 on success or -1 on fail.
4
2215
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 process? 2. What information contained in sn key. I gone through that it is having public key. How it is using this key to intract with client....
13
15531
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 HTTP level but as yet no fully useful answer. I too have been trying to call an apache/axis webservice which desires a username/password from my C#...
5
1800
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 property '.fulladdress' to be the value that appears in the drop downs text section. How to I set that parameter to be the one shown inthe drop down
4
1715
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, - Programming Visual Basic by Balena (MS Press) and - Visual Basic 2005 by Willis (WROX), but they don't go into the forms design aspects and...
1
1479
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 visual basic applicaton that runs on his server on the remote terminals. (he has two networked to his server) My question is do I now have to install...
4
3078
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 applications into Visual Basic.Net. My managers main thought is that Visual Basic 6 is (or has!) stopped being supported by Microsoft.
3
1930
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 objects that I can iterate through relatively easily. I've included code at the bottom of this message. I can pretty easily iterate through my...
0
7410
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7923
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4960
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.