Public Group

# Changing the delete operator

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

## 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 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 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 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 on other sites
I find it quite useful..

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

##### Share on other sites
Quote:
 Original post by bobason456I 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 on other sites
Quote:
 Original post by bobason456I 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 on other sites
Quote:
Original post by Conner McCloud
Quote:
 Original post by bobason456I 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 on other sites
Quote:
Original post by Conner McCloud
Quote:
 Original post by bobason456I 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 on other sites
Quote:
Original post by rip-off
Quote:
Original post by Conner McCloud
Quote:
 Original post by bobason456I 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...

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 10
• 10
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
631752
• Total Posts
3002090
×