Sign in to follow this  
raptorstrike

Changing the delete operator

Recommended Posts

Well I was reading through a post today and this just hit me: Why hasnt anyone made it so that when you call delete the pointer which is on the right side is set to zero? it makes sence and there are very few instances where you would not want the pointer to revert back to zero, it would take more processing time but then again if you are really deleting/allocating that many pointers at runtime it probably bad design anyway. [Edited by - raptorstrike on March 5, 2006 4:26:01 PM]

Share this post


Link to post
Share on other sites
What would you gain from setting the pointer to zero? Anyway, you wouldn't be able to set the pointer to NULL because, unless I am mistaken, the delete operator operates on a void *. So, you would only be setting the pointer to NULL within the operator delete function call and not from the calling function.

Share this post


Link to post
Share on other sites
One big reason is because it would provide no real safety. Consider:

int* a = new int(5);
int* b = a;
delete a;
if(b)
*b = 16; //oops


Also consider that it would require that delete take a non-const reference instead of a value. I'm not entirely certain this is allowed.

CM

Share this post


Link to post
Share on other sites
Yes, you can do this. In fact, IIRC the Standard suggests it. However, as mentioned, the fact that delete needs to work on a non-lvalue and that multiple pointers may refer to the deleted object makes this of limited usefulness.

Share this post


Link to post
Share on other sites
Quote:
Original post by bobason456
I find it quite useful..

#define __delete(p) if(p!=NULL){ delete p; p = NULL;}


IIRC, delete knows what The Right Thing To Do is with a null pointer.

Share this post


Link to post
Share on other sites
Quote:
Original post by bobason456
I find it quite useful..

#define __delete(p) if(p!=NULL){ delete p; p = NULL;}

The behavior of "delete 0;" is perfectly well defined, and does nothing. Also, double underscores are against the rules. I'm fairly certain this applies to macros, and even if not its a good practice to avoid. And you've accessed p three times in a single macro. Bad form, what if p has side effects?

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by Conner McCloud
Quote:
Original post by bobason456
I find it quite useful..

#define __delete(p) if(p!=NULL){ delete p; p = NULL;}

The behavior of "delete 0;" is perfectly well defined, and does nothing. Also, double underscores are against the rules. I'm fairly certain this applies to macros, and even if not its a good practice to avoid. And you've accessed p three times in a single macro. Bad form, what if p has side effects?

CM


Yes, yes:

template<class T>
inline void delete_(T *& p) {
delete (p);

p = NULL;
}



Note that this means you can't do:

delete_(0xA0000000);


But then again, you won't be deleting straight addresses. More importantly, you don't want to delete your VGA video memory....

Share this post


Link to post
Share on other sites
Quote:
Original post by Conner McCloud
Quote:
Original post by bobason456
I find it quite useful..

#define __delete(p) if(p!=NULL){ delete p; p = NULL;}

The behavior of "delete 0;" is perfectly well defined, and does nothing. Also, double underscores are against the rules. I'm fairly certain this applies to macros, and even if not its a good practice to avoid. And you've accessed p three times in a single macro. Bad form, what if p has side effects?

CM


I thought so too( about the double underscores ), but seeing as this code will be substituted before the compiler gets its hands on it, it shouldn't cause a problem( unless you define it before you include a header that could use it, for example a STL header ).

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Quote:
Original post by Conner McCloud
Quote:
Original post by bobason456
I find it quite useful..

#define __delete(p) if(p!=NULL){ delete p; p = NULL;}

The behavior of "delete 0;" is perfectly well defined, and does nothing. Also, double underscores are against the rules. I'm fairly certain this applies to macros, and even if not its a good practice to avoid. And you've accessed p three times in a single macro. Bad form, what if p has side effects?

CM


I thought so too( about the double underscores ), but seeing as this code will be substituted before the compiler gets its hands on it, it shouldn't cause a problem( unless you define it before you include a header that could use it, for example a STL header ).


or unless a header you include before #define __delete it also defines __delete.

So as long as you use it neither before, nor after, a standard header you'll be fine. How useful...

Share this post


Link to post
Share on other sites
Quote:
Original post by raptorstrike
Well I was reading through a post today and this just hit me: Why hasnt anyone made it so that when you call delete the pointer which is on the right side is set to zero? it makes sence and there are very few instances where you would not want the pointer to revert back to zero


Sometimes I want that pointer being set to zero, but other times I am just deleting the structure that held the pointer anyway (such as in an std::list or whatever) so it would be a waste of time. I'd rather not have C++ do extra 'safety' work that I don't need. I have other languages I use when I want that. When I use C++ I want to know that it's generating efficient code.

Besides which, automatically setting the pointer to 0 can hide bugs. Often, you should know whether a pointer is valid or not, and filling your code with lines that some places that set pointers to 0 and in other places repeatedly checking for 0 pointers is often a sign that your program logic isn't very clear. That's not to say that I never do it, but that it's the kind of code that should be kept to a minimum.

Quote:
if you are really deleting/allocating that many pointers at runtime it probably bad design anyway.


No, not really. Most non-trivial applications will make many, many such allocations throughout their lifetime.

Share this post


Link to post
Share on other sites
Quote:
Original post by Conner McCloud
Quote:
Original post by bobason456
I find it quite useful..

#define __delete(p) if(p!=NULL){ delete p; p = NULL;}

The behavior of "delete 0;" is perfectly well defined, and does nothing. Also, double underscores are against the rules. I'm fairly certain this applies to macros, and even if not its a good practice to avoid. And you've accessed p three times in a single macro. Bad form, what if p has side effects?

CM


Just showing off my inexperience with C / C++ in general again..

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