Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ Programming
➜ General
➜ Use of new/delete
It is now over 60 days since the last post. This thread is closed.
Refresh page
| Posted by
| Samson
USA (683 posts) Bio
|
| Date
| Sun 27 Feb 2005 09:08 PM (UTC) |
| Message
| I have a question, which arose partly out of fixing some crash bugs which brought certain things to light.
When you use new/delete, and you call delete on a class/struct/whatever, is it necessary to then NULL the pointer after calling delete?
I had a situation where stop_editing, stop_hunting, stop_fearing, and other functions of this nature were not quite finishing the job of removing the memory pointer.
In stop_editing, I had this:
delete ch->pcdata->editor;
If you used the editor once, no problem. Any attempt to use it a second time and it would crash unless I did this:
delete ch->pcdata->editor;
ch->pcdata->editor = NULL;
Now, obviously I've not done this for every last use of delete, so I'm wondering if I need to. Gotta hate how you discover this kind of thing *AFTER* you've gone through and changed almost everything to use them :P | | Top |
|
| Posted by
| David Haley
USA (3,881 posts) Bio
|
| Date
| Reply #1 on Sun 27 Feb 2005 11:16 PM (UTC) |
| Message
| Delete does not set the pointer to null, if that's what you mean. So, you have to set it to null yourself if you need to mark that the pointer is no longer valid.
Here's a little something I use from time to time to save some typing:
template<typename T>
void ClearPointer(T* & ptr)
{
if ( ptr )
delete ptr;
ptr = NULL;
}
You don't need to specify the template type, it can deduce it on its own.
So the following will work:string * s = new string("hello");
cout << *s << endl; // "hello"
ClearPointer(s);
cout << s << endl; // null
All that being said, if you're deadling with arrays using new[], don't forget to delete using delete[]. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | | Top |
|
| Posted by
| Samson
USA (683 posts) Bio
|
| Date
| Reply #2 on Sun 27 Feb 2005 11:56 PM (UTC) |
| Message
| Blah. I was afraid you'd say something like that. So now I have to go through and make sure all my delete calls are in line. Bleh :P
That little template thing is nice though, I think I'll snag that and put it to use :) | | Top |
|
| Posted by
| Raz
(32 posts) Bio
|
| Date
| Reply #3 on Thu 03 Mar 2005 01:04 AM (UTC) |
| Message
| template<typename T>
void ClearPointer(T* & ptr)
{
if ( ptr )
delete ptr;
ptr = NULL;
}
The if statement is unecessary. delete and delete [] will safely work if ptr is NULL. |
-Raz
C++ Wiki: http://danday.homelinux.org/dan/cppwiki/index.php | | Top |
|
| Posted by
| David Haley
USA (3,881 posts) Bio
|
| Date
| Reply #4 on Thu 03 Mar 2005 04:54 AM (UTC) |
| Message
| | I didn't know that delete was null-safe. I think it makes the code more readable, though, with the if statement. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | | Top |
|
| Posted by
| Nick Gammon
Australia (23,173 posts) Bio
Forum Administrator |
| Date
| Reply #5 on Wed 23 Mar 2005 08:03 PM (UTC) |
| Message
| I think there is a good reason why delete doesn't clear the pointer, for one thing the pointer may be const.
Raz is right, delete can take a null pointer, which makes code a bit cleaner, however you still have the issue with deleting arrays.
Having said that these days I would recommend using STL and avoiding manually setting up arrays (of pointers) in the first place. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | | Top |
|
| Posted by
| Raz
(32 posts) Bio
|
| Date
| Reply #6 on Thu 24 Mar 2005 01:51 AM (UTC) |
| Message
|
Quote: Having said that these days I would recommend using STL and avoiding manually setting up arrays (of pointers) in the first place.
On top of that advice, using Boost's smart pointer library (http://www.boost.org/libs/smart_ptr/smart_ptr.htm) simplifies a lot of the work. |
-Raz
C++ Wiki: http://danday.homelinux.org/dan/cppwiki/index.php | | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
22,573 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top