简体   繁体   中英

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:

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)

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指针执行任何其他操作一样,除非将其显式转换为另一种指针。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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