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

后端 未结 5 1275
星月不相逢
星月不相逢 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:04

    It will deallocate with correct size, because the size to be deallocated is a property of the heap memory region you obtained (there is no size passed to free()-like functions!).

    However, no d'tor is called. If 'B' defines a destructor or contains any members with a non-trivial destructor they will not be called, causing a potential memory leak. This is not the case in your code sample, however.

提交回复
热议问题