Will using delete with a base class pointer cause a memory leak?

后端 未结 5 1148
星月不相逢
星月不相逢 2021-02-05 08:38

Given two classes have only primitive data type and no custom destructor/deallocator. Does C++ spec guarantee it will deallocate with correct size?

struct A { in         


        
5条回答
  •  难免孤独
    2021-02-05 09:14

    Unless the base class destructor is virtual, it's undefined behaviour. See 5.3.5/4:

    If the static type of the operand [of the delete operator] 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 behaviour is undefined.

提交回复
热议问题