Jump to content
  • Advertisement
Sign in to follow this  
jflanglois

nulling a deleted pointer

This topic is 5043 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I was wondering whether it was possible to replace the global delete operator so that it will set the deleted pointer to null. If I do:
void operator delete( void *p ) {
  free( p );
  p = 0;
}


it doesn't work because I am passing the pointer by value, yet when trying to do "void operator delete( void *&p )", the compiler (VC++.NET2003, though I suppose this shouldn't matter) screams at me saying that it should be void *. Any ideas? Note: This is mostly to satisfy my curiosity, since I intend to move over to weak/strong safe pointers when I get the time/inclination. Thanks in advance for any assistance. Regards, jflanglois

Share this post


Link to post
Share on other sites
Advertisement
I'm pretty sure that this can't be done, and even if you could do it, you shouldn't. delete can be called on pointers that aren't lvalues (like the return value of of functions). This would mean that perfectly happy delete expressions would all of the sudden not work.

edit: formatting

[Edited by - SiCrane on December 4, 2004 2:02:42 AM]

Share this post


Link to post
Share on other sites
I somewhat common "solution" in C is something like

#define FREE(x) do { free(x); x = NULL; } while (0)

There might be a better way to define it, I haven't really looked into it because it doesn't "really" work. Consider:

#include <stdlib.h>
#include <stdio.h>
#define FREE(X_) do { free(X_); X_ = NULL; } while (0)
int main(void) {
int *b = malloc(sizeof *b);
int *c = b;
FREE(b);
if (!c) { puts("Problem solved!"); } /* Thanks Nice Coder */
else { puts("Problem NOT solved!"); }
}

You could adapt the macro to work with delete/delete[] so it's more applicable to C++, but you'll still have the problem above.

[Edited by - Way Walker on December 4, 2004 1:12:05 AM]

Share this post


Link to post
Share on other sites
Yeah, I sometimes use those types of macros. This was just a question born out of curiosity, as I wrote before. Thanks for the suggestion though.

Regards,
jflanglois.

Share this post


Link to post
Share on other sites
Bad idea. If the pointer points to an object, you need to call it's destructor. free doesn't do that.

Share this post


Link to post
Share on other sites
Quote:
Original post by smr
Bad idea. If the pointer points to an object, you need to call it's destructor. free doesn't do that.


Are you refering to my macro or jflanglois' operator delete? If my macro, you should reread my post and see that I made a distinction between C/C++. If jflanglois' operator delete, then, forgive me if I'm wrong, but I was under the impression that the destructor is not the responsibility of operator delete but something that compiler does "automagically" when you use delete on an object?

Share this post


Link to post
Share on other sites
Quote:
Original post by Way Walkerbut I was under the impression that the destructor is not the responsibility of operator delete but something that compiler does "automagically" when you use delete on an object?


The key portion of that statement would be "when you use *delete*"... Doesn't happen when you use free.

- Neophyte

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!