Deleting pointer sometimes results in heap corruption

前端 未结 7 627
深忆病人
深忆病人 2021-01-07 03:34

I have a multithreaded application that runs using a custom thread pool class. The threads all execute the same function, with different parameters.

These parameters

7条回答
  •  南笙
    南笙 (楼主)
    2021-01-07 04:08

    Using operator delete on pointer to void results in undefined behavior according to the specification.

    Chapter 5.3.5 of the draft of the C++ specification. Paragraph 3.

    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.73)

    And corresponding footnote.

    This implies that an object cannot be deleted using a pointer of type void* because there are no objects of type void

提交回复
热议问题