Will using delete with a base class pointer cause

2019-02-04 09:31发布

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 { int foo; };
struct B: public A { int bar[100000]; };
A *a = (A*)new B;
delete a;

I want to know do I need to write an empty virtual dtor?

I have tried g++ and vc++2008 and they won't cause a leak. But I would like to know what is correct in C++ standard.

5条回答
冷血范
2楼-- · 2019-02-04 09:43

For only primitive data I believe you're fine. You might legitimately not want to incur the cost of a v-table in this case. Otherwise, a virtual d'tor is definitely preferred.

查看更多
放我归山
3楼-- · 2019-02-04 09:49

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.

查看更多
来,给爷笑一个
4楼-- · 2019-02-04 09:51

This is undefined behaviour - maybe everything's fine, maybe whetever goes wrong. Either don't do it or supply the base class with a virtual destructor.

In most implementations this will not leak - there're no heap-allocated member functions in the class, so the only thing needed when delete is done is to deallocate memory. Deallocating memory uses only the address of the object, nothing more, the heap does all the rest.

查看更多
相关推荐>>
5楼-- · 2019-02-04 10:00

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.

查看更多
Bombasti
6楼-- · 2019-02-04 10:01

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.

查看更多
登录 后发表回答