Sign in to follow this  
jflanglois

nulling a deleted pointer

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
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
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
If you implement delete in terms of free(), then, provided that the rest of your program is well-formed, there should be no problems since by the time the free() is called, the compiler will have taken care of calling the destructor.

Share this post


Link to post
Share on other sites
Quote:
Original post by Neophyte
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


I don't believe I (or anyone else in this thread) ever said otherwise. I was trying to figure out what smr thought was a bad idea.

free() is still an appropriate way to implement operator delete (assuming operator new is implemented using malloc()).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this