Sets a pointer to zero when deleting
Why doesn't delete set the pointer to NULL?
I've always wondered why the pointer automatically adjusts to zero after that Clear does not belong to the standard. When this is fixed, many of the invalid pointer crashes will not occur. However, I can think of a few reasons why the standard would have restricted this:
An additional instruction can affect performance.
Could it be due to pointers.
On the other hand, Standard could have done something for this special case.
Does anyone know the exact reasons for not allowing this to happen?
Stroustrup himself answers. A section:
C ++ explicitly allows an implementation of delete to set an lvalue operand to zero, and I was hoping implementations would, but this idea doesn't seem to have become popular with implementers.
The main problem it poses, however, is that the argument to delete need not be a value.
First, setting to zero would require a variable stored in memory. It is true that you usually have a pointer in a variable, but sometimes you might want an object at an address just calculated Clear . That would be impossible to delete with "cancel".
Then comes the performance. You may have written your code so that the pointer immediately follows the Delete the Scope leaves . Zeroing it is just a waste of time. And C ++ is a language with the ideology "You don't need it? Then you don't have to pay for it".
When you need security, there are plenty of smart pointers available or you can write your own - better and smarter.
You can have multiple pointers pointing to this memory. It would create a false sense of security if the pointer you specified for deletion were set to zero but all other pointers were not. A pointer is nothing more than an address, a number. It might as well be an int with a indirection operation. My point is that you'd also have to scan every single pointer to find the ones that point to the same memory you just cleared and zero them as well. It would be computationally intensive to search through all pointers for this address and set it to zero because the language was not designed for that. (Although some other languages structure their references to accomplish a similar goal in different ways.)
A pointer can be stored in more than one variable. If you set one of these pointers to NULL, there are still invalid pointers left in the other variables. So you don't really gain much, but rather create a false sense of security.
Also, you can create your own function that does what you want:
Because it isn't really necessary, and because deleting would require taking pointer to pointer, and not just pointer.
is mainly used in destructors. In this case, there is no point in setting a member to NULL. A few lines later, at the end, the member no longer exists. With assignment operators, a deletion is usually followed by an assignment anyway.
Also, the following code would be illegal:
Here's another reason; Suppose delete sets its argument to NULL:
Should the bar be set to NULL? Can you generalize that?
If you have an array of pointers and your second action is to clear the empty array, there is no point in setting each value to zero when trying to free the memory. If you want it to be zero write zero on it :)
In C ++ you can redefine and delete your own operator so that it uses your own pool allocator, for example. When you do this, it is possible to use new and delete with things that are not strictly addresses but contain indexes in your pooled array. In this context, the value of NULL (0) can have a legal meaning (related to the first element in the pool).
So if delete automatically set NULL to its argument, it doesn't always mean - set the value to an invalid value. The invalid value might not always be NULL.
The philosophy of C ++ is "only pay when you use it". I think it might answer your question.
Sometimes you could also have your own heap that recovers deleted memory. Or sometimes a pointer that doesn't belong to any variable. Or pointers stored in a few variables - it is possible to zero just one of them.
As you can see, there are many problems and potential problems.
Setting the pointer to NULL automatically would not solve most problems with poor pointer usage. The only crash it would avoid is if you try to delete it twice. What if you call a member function on such a pointer? It would still crash (assuming it was accessing member variables). C ++ does not and should not prevent you from calling NULL pointer functions.
I see people giving strange answers to this question.
ptr = NULL; How can such a simple statement lead to delays in performance?
Another answer is that multiple pointers can point to the same location. Sure we can. In this case, a delete operation on a pointer would only make that pointer NULL (if delete makes the pointer NULL) and the other pointer would not be NULL and would point to the free location.
The solution to this should be that the user should delete all pointers that point to the same position. It should be checked internally whether the memory has already been released as not released. Just make the pointer ZERO.
Stroustrup could have designed the deletion that way. He thought programmers would take care of it. So he ignored it.
- What are the national symbols of Palestine
- How do I appreciate abstract modern art?
- How did the butterfly metamorphosis develop
- Why is there no weight loss
- Is Irish coffee better than cappuccino
- Which man has the most beautiful eyes
- What is descriptive linguistics
- What is the value of 105 1
- What makes you feel satisfied and safe?
- What do you think of socialism
- Which fountain pen do you use
- What is that famous food in Lucknow
- How many basic human rights are there
- How does WPS Office make money
- When were bacteria first discovered?
- What is a conditional operator
- What is a website for Stock ROM
- What social rules do people break often?
- How does the Gimbal Context Awareness Platform work
- Supports every Android phone 4G
- Will Scala stand out
- How was the caste system formed in India?
- Why was the Second World War so long and difficult
- How nihilists raise their children
- How did you treat your migraine headache
- Can I have a baby when I am 18
- Why were Obama's deportations not controversial
- School dropouts can join the Marines
- What is the bond of self
- Berber Amazighs have an ancient civilization
- How can a DNA be destroyed
- Has President Bush ever visited Cuba
- KIIT accepts the CBSE improvement stamp sheet
- How can I sing from the diaphragm