Is it possible to remove nullptr in C ++ 0x?

In c++03 it is pretty clear that deleting a null pointer has no effect. In fact, in §5.3.5/2 explicitly stated that:

In any alternative, if the value of the delete operand is a null pointer, the operation has no effect.

However, in the current project for c++0x this suggestion seems to be missing. In the rest of the project, I could only find suggestions about what would happen if the operand of the delete expression is not a null pointer constant. Removes a null pointer still defined in c++0x , and if so, where?

Notes:

There is significant indirect evidence that they are still clearly defined.

Firstly, in §5.3.5/2 there are two sentences which state that

In the first alternative (delete object), the value of the delete operand may be the value of a null pointer, ...

and

In the second alternative (delete array), the value of the delete operand may be the value of a null pointer or ...

They say that the operand is allowed to be zero, but they themselves do not determine what will happen if it is.

Secondly, changing the value of delete 0 is a serious violation of the changes, and the standards committee is unlikely to change this particular change. Moreover, the mention of this is not a change in the compatibility application (Appendix C) of the c++0x project. However, Appendix C is an information section, so this has nothing to do with the interpretation of the standard.

On the other hand, the fact that deleting a null pointer is not required implies additional verification at runtime. In a lot of code, an operand can never be null, so this runtime check contradicts the principle of a zero premium. Perhaps the committee simply decided to change the behavior to bring standard C ++ into line with the stated design goals of the language.

+57
c ++ null language-lawyer delete-operator
Jul 18 2018-11-11T00:
source share
2 answers

5.3.5 / 7 says:

If the operand value of the delete-expression is not a null pointer value, the expression-expression will call the release function (3.7.4.2). Otherwise, it is not clear whether the release function will be called.

And 3.7.4.2/3 says:

The value of the first argument provided to the release function may be a null pointer value; if so, and if the release function is included in the standard library, the call has no effect.

Thus, the behavior is correctly defined if the standard redistribution function is used, or if the user-provided deallocation function correctly processes null pointers.

+72
Jul 18 '11 at 10:21
source share

On the other hand, the fact that deleting a null pointer is not required implies additional checking of the runtime.

The new wording does not remove this runtime check for a null pointer. Another thing: the draft standard is approaching the assertion that the implementation should make the test with a null pointer compatible.

It should also be noted: the old standard contradicted itself by saying (5.3.5 / 2) that “if the value of the delete operand is a null pointer, the operation has no effect”, but later said that (5.3.5 / 7) "delete-expression will call the release function." A function call is an effect. This is especially true since the function being called may well be overridden by operator delete .

The new formulation eliminates this contradiction by explicitly leaving it to the implementation whether the release function is called when the null pointer is deleted.

+5
Jul 18 '11 at 10:52
source share



All Articles