简体   繁体   English

C ++删除static_cast <void *>(指针)行为

[英]C++ delete static_cast<void*> (pointer) behavior

suppose the code does the following: 假设代码执行以下操作:

T *pointer = new T();
delete static_cast<void*>(pointer);

what is result? 结果是什么? Undefined, memory leak, memory is deleted? 未定义,内存泄漏,内存被删除?

The behavior is undefined. 行为未定义。 Concerning the delete expression, the C++ standard says: 关于删除表达式,C ++标准说:

In the first alternative ( delete object ), if the static type of the operand is different from its dynamic type, the static type shall be a base class of the operand's dynamic type 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. 在第二个备选( 删除数组 )中,如果要删除的对象的动态类型与其静态类型不同,则行为未定义。 (§5.3.5/3) (§5.3.5/ 3)

Then the footnote to this paragraph clearly states: 然后,本段的脚注明确指出:

This implies that an object cannot be deleted using a pointer of type void* because there are no objects of type void (note 73). 这意味着无法使用void*类型的指针删除对象,因为没有void类型的对象(注释73)。

通过void指针删除是未定义的,就像通过void指针执行任何其他操作一样,除非将其显式转换为另一种指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM