Well I saw 12 answers, and 11 gave cons to the idea. Nimesh Bapodra' answer was the only one pro to the idea.
After reading all the answers and comments and put thought into it; I am 100% for the idea! Lets recap the reasons against the idea.
- Don't pay for what you don't use
- False positives if multiple pointers share memory
- Setting the values of const/temporary pointers
- C++ allows overload of operator delete
- It is not necessary
It is not necessary
Well I guess checking if a pointer is not null is also unnessary? Garbage collectors in Java/C# is unnecessary. Like what is the sense in the compiler helping us to keep our code safe? Might as well remove const keyword while we are at it. That is too much typing.
C++ allows overload of operator delete
Well guess what? The built in version can keep us safe. And your version can do whatever it likes.
False positives if...
False positives if multiple pointer shared the same memory. At first it seems to make sense; but when you think couple steps further you see it absolutely makes no sense. What false positive? If you are sharing memory across pointers, you shouldn't be relying on nullpointer checks to determine if the memory is valid. You should be checking a reference counter. If, just if, your logic wants to depend on nullpointer checks; you would have to IMMEDIATELY set to NULL everyother pointer! else your darn state machine would be in an invalid mess... ANYHOW!!!! Furthermore, who is going to waste cpu cycles to set all those pointers NULL?
Setting the value of const pointers
This seems reasonable. But only if you are a novice to programming. The C++ abstract machine is allowed to do anything it wishes. It has god like powers in its realm. It is master and we are the servants. It can decide to discard UB code. It can decide to give UB code a bly in life. There is nothing stopping the compiler from modifying const pointers. const pointers are nothing more than pointers with a const attribute. You can remove that with a const_cast. The compiler could do it without a sweat.
Don't pay for what you don't use
This is the major concern for most people. But there is a simple fool proof work around. 1. Guarantee that all pointers will become NULL when deleted(built in version). 2. Break the guarantee aka optimize, if nulling the pointer is not necessary. Such as no more access to pointer before it looses scope. That one is easy to implement. Any deleted pointer still in scope being accessed is UB anyhow(except for equality operators, I assume). And on the same note, I think Cpp should guarantee that local variables be initialized to zero. But then optimize the code to not initialize local variables which were explicitly initialized before use.
But here is the catch... As I put in this long work putting this together and thinking about it; I came up with two problems no bother mentioned. Worst yet, I have no solution for one of them.(Or so I thought).
Would break backward compatibility
Someone with a const pointer, I dont know how much of you exist, will suffacate if his const pointer to memory is all of a sudden NULL. Let's say he intended to exit this function and use said pointer to remove it from a reference count map. Well I think code that rely on deleted address should BREAK. Either you manually save the address in another variable before deleting or you just update the reference count map in this function before deleting the pointer. I really think a safety feature like this is more important.
Setting the value of temporary pointers
At the get go, my simple answer was, well in this case, Cpp doesnt have to nullify anything. Then I thought about what the expression delete (pointer+1) mean.
char **pointer = new pointer*[4];
delete (pointer+1);
In a world where the Cpp abstract machine nullify deleted pointers, one can easily be tricked into thinking that pointer[1] == NULL. And they would be DARN RIGHT. Cpp abstract machine can look at this code and see that pointer is not a temporary, and we to delete the next element of the array. And simply just nullyfy pointer[1]. But check out this next example.
class String
{
char *begin = new char[1024];
char *end = s.begin + 1024;
public: char* getBegin() { return begin; }
};
String s;
delete[] getBegin();
What to do now? Well I think that by encapsulating String::begin into a function, you have encapsulated it; and have no ability to modify it. If you returned a reference, or a pointer to a pointer, then the delete operator could dereference it and nullify the target.
In Summary
I am 100% in agreement with Cpp delete operators(built in) nullifying pointers. It will make Cpp a safer place. I also recommend that Cpp initialize primitive local variables.