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

后端 未结 5 1133
星月不相逢
星月不相逢 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 08:53

    According to the C++ standard, what you have is undefined behaviour - this may manifest itself as a leak, it may not, For your code to be correct you need a virtual destructor.

    Also, you do not need that (A*) cast. Whenever you find yourself using a C-style cast in C++, you can be fairly sure that either it is unecessary, or your code is wrong.

提交回复
热议问题