c – 使用与new中使用的类型不同的指针删除内存是否安全?
作者:互联网
以下代码是否安全?有没有提到C标准来解决这个问题?
// SomeStruct is POD: no constructors or destructor
SomeStruct *pSS = new SomeStruct();
void *pV = reinterpret_cast<void*>(pSS);
delete pV;
解决方法:
这只适用于:
>你删除一个指向基地的指针,
>并且该基类具有虚拟析构函数.
否则,你处于非法代码和未定义行为的境地.
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 ofdelete
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 bynew
, 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.
另外,void是一种不完整的类型(C 14 3.9.1 / 9):
The
void
type has an empty set of values. Thevoid
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 cvvoid
(5.4). An expression of typevoid
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 oftypeid
,noexcept
, ordecltype
, as the expression in a return statement (6.6.3) for a function with the return typevoid
, or as the operand of an explicit conversion to type cvvoid
.
此外,除非您与C API接口,否则您应该努力避免使用void *.
标签:c,heap-memory,new-operator,delete-operator 来源: https://codeday.me/bug/20190829/1762977.html