Serge Rogatch
Serge Rogatch

Reputation: 15070

Is it safe to delete memory with a pointer of different type than the used in new?

Is the following code safe? Is there a reference to C++ standard addressing this question?

// SomeStruct is POD: no constructors or destructor
SomeStruct *pSS = new SomeStruct();
void *pV = reinterpret_cast<void*>(pSS);
delete pV;

Upvotes: 3

Views: 156

Answers (2)

rubenvb
rubenvb

Reputation: 76720

This is only OK when:

  1. you delete a pointer-to-base,

  2. and that base class has a virtual destructor.

Otherwise, you're in the land of illegal code and undefined behaviour.

C++14 5.3.5/2

If the operand has a class type, the operand is converted to a pointer type by calling the above-mentioned conversion function, and the converted operand is used in place of the original operand for the remainder of this section. In the first alternative (delete object), the value of the operand of delete may be a null pointer value, a pointer to a non-array object created by a previous new-expression, or a pointer to a subobject (1.8) representing a base class of such an object (Clause 10). If not, the behavior is undefined. In the second alternative (delete array), the value of the operand of delete may be a null pointer value or a pointer value that resulted from a previous array new-expression. If not, the behavior is undefined. [ Note: this means that the syntax of the delete-expression must match the type of the object allocated by new, not the syntax of the new-expression. — end note ] [ Note: a pointer to a const type can be the operand of a delete-expression; it is not necessary to cast away the constness (5.2.11) of the pointer expression before it is used as the operand of the delete-expression. — end note ]

C++14 5.3.5/3

In the first alternative (delete object), if the static type of the object to be deleted is different from its dynamic type, the static type shall be a base class of the dynamic type of the object to be deleted and the static type shall have a virtual destructor or the behavior is undefined. In the second alternative (delete array) if the dynamic type of the object to be deleted differs from its static type, the behavior is undefined.

Additionally, void is an incomplete type (C++14 3.9.1/9):

The void type has an empty set of values. The void type is an incomplete type that cannot be completed. It is used as the return type for functions that do not return a value. Any expression can be explicitly converted to type cv void (5.4). An expression of type void shall be used only as an expression statement (6.2), as an operand of a comma expression (5.19), as a second or third operand of ?: (5.16), as the operand of typeid, noexcept, or decltype, as the expression in a return statement (6.6.3) for a function with the return type void, or as the operand of an explicit conversion to type cv void.


Also, unless you're interfacing with a C API, void* is something you should strive to avoid completely.

Upvotes: 7

SergeyA
SergeyA

Reputation: 62603

Not only it is illegal, it is simply a compilation error on modern compilers. Can't delete void*.

Upvotes: -2

Related Questions